Is typing good in python?

I know that python essentially is a duck typed language and, in a way, asking for type checking is a no-brainer considering there is no plan to make cpython statically typed.

The point I am not understanding is, being optional and helpful in finding bug and better documentation, why is type hinting (PEP-484 and followups) not popular among tutorials and the community in general?

I am not talking about tutorials aimed at complete beginners. Consider function docstrings. There comes a state when we start documenting argument types for a function (e.g. see the numpy's guide on parameters), but most of us don't use type hints which can help both in documentation + type checking (using mypy, say) and instead use plain docstrings. Is it because of the idea being slightly contrary to python's dynamic typing rise to fame? Or is it simply not known by many?

EDIT: I mean type hinting (which translates to comment based hinting for Python 2) and not just type annotations for Python 3.

Improve your code without impact your performance.

Is typing good in python?

Photo by Sangga Rima Roman Selia on Unsplash.

When I started to learn programming, I used C as the main language. Declaring the actual type of the variable you will use was mandatory, and the compiler would complain a lot if you forget. Changing to C++ and Java later didn’t change anything. I still needed to know which type I was going to use…

Is typing good in python?

Image creates with carbon.now.sh

Before switching to software engineering, I was working on data science projects. One surprising thing is that, despite having worked on creating a Python package for actuaries, I had never heard of type hints.

Then, I had the opportunity to work on large Python projects and that is when I discovered type hints. As a daily user of Python for 3–4 years, I found it confusing to use type hints when Python is known to be a dynamically typed language. After digging into the subject, I realized the power of type hints. Through this article, this is what I will try to make you discover: what is typing, what are type hints, how do you enforce it and why do you need it or not.

1. Static typing vs Dynamic typing

Static typing

Static typing consists in declaring within programs the type of variables or class attributes at the same time as their identifier. This anticipates low-level problems but also ensures a certain degree of programming security by introducing additional rigour and allowing the compiler to detect more potential problems.

An example in C:

int number;number = 1;

The first line declares that the variable numberis bound to the int type at compile time. The variable can never be rebound to another type.

Dynamic typing

Dynamic typing allows greater flexibility (e.g. changing the type of a variable at runtime) but is necessarily less efficient.

However, it has the advantage of being much more manageable and allows classical problems to be solved in a much more natural and elegant way.

An example in Python:

number = 7.77print(type(number))>>> number = '12345'print(type(number))>>> 

In this example, the type() function returns the type of an object. It confirms that the type of number is allowed to change, and Python correctly infers the type as it changes.

2. Python’s typing module

Introduced in Python 3.0 and in the standard library since Python 3.5, Python’s typing module contains the main type hints.

Python is a high level language with dynamic typing. In fact, it determines the type of objects during run-time. This is why it is sometimes hard for developers to find out what exactly is going on in the code.

In order to reduce and mitigate this concern, the Python’s typing module has introduced type indications. Still called type annotations, they are a good way for the programmer to indicate the type of the object used (during compilation) and to help external type checkers. This makes the code more readable, more robust and better documented for other developers.

Any type error is identified when an external type checker is executed. It is not intended to check the type at compile time or runtime. For example, if the object returned by Python is not the same type as the one specified, there will be no compile-time error. The most common tool for doing type checking is Mypy (Dropbox). But it is also possible to use Pytype (Google), Pyright (Microsoft) or Pyre (Facebook).

⚠️ Thus, it is important to note that typing in Python is only code documentation and help for external type checkers. It does not make Python a statically typed language.

3. Type hints — Annotations

Introduction

In order to implement type hints in your Python code, you will need to use what are called annotations. This notion of annotation was introduced in Python 3.0, originally without any specific purpose.

In September 2014, Guido van Rossum (Python BDFL), thanks to the work Jukka Lehtosalo had done on his PhD project (Mypy), created a Python Enhancement Proposal, PEP-484 to add type hints to Python through annotations.

Type annotations

Type annotations is the straightforward way and is the one you’ll find mostly mentioned on the Python’s typing documentation. It uses function annotations added to language with PEP-3107 (Python 3.0+) and variable annotations with PEP-526 (Python 3.6+).

These allow you to use the : syntax to attach information to variables and function arguments. The -> operator is used to attach information to the return value of a function/method.

Function Annotations

Thus, the syntax to annotating a function (its arguments and the return value) is as follows:

⚠️ The annotation must be a valid Python expression.

To better understand, let’s take as an example a function sphere_volume() that calculates the volume of a sphere. Let’s start by writing this function without annotations:

Now, the same function but with annotations:

Variable Annotations

In the definition of sphere_volume(), in the previous section, we only annotated the arguments and the return value. But it is also possible to annotate the variablepi:

4. Mypy, a static type checker

Is typing good in python?

Image from https://github.com/python/mypy

What is Mypy ?

Mypy is an optional static type checker for Python that aims to combine the benefits of dynamic (or “duck”) typing and static typing. Mypy combines the expressive power and convenience of Python with a powerful type system and compile-time type checking. Mypy type checks standard Python programs.

How to install and run Mypy ?

Mypy requires Python 3.5 or later to run. Once you’ve installed Python 3, install Mypy using pip:

$ python3 -m pip install mypy

Once Mypy is installed, run it by using the Mypy tool:

$ mypy program.py

This command makes Mypy type check your program.py file and print out any errors it finds. Mypy will type check your code statically: this means that it will check for errors without ever running your code, just like a linter.

Example with the sphere_volume() function

If we run the example through Python and Mypy:

$ mypy sphere_volume_mypy_example_1.pySuccess: no issues found in 1 source file

In the terminal output, Mypy tells us that there are no issues.

Now, if you have errors, for instance if you call sphere_volume() with radius="7.77", Mypy will show:

$ mypy sphere_volume_mypy_example_2.pysphere_volume.py:7: error: Argument 1 to “sphere_volume” has incompatible type “str”; expected “float”Found 1 error in 1 file (checked 1 source file)

5. How to use Typing on an existing project

MyPy supports the concept of gradual typing. This means that you can gradually introduce types into your code. Code without type hints will be ignored by the static type checker. Therefore, you can start adding types to critical components, and continue as long as it adds value to you.

There are also type annotation generators. When you want to add type annotations to an existing code base, use them to automate the most boring part.

6. Pros and Cons of using type hints and static type checkers

Why should you use type hints and static type checkers ?

Type hints:

  • Help document your code.
  • Improve IDEs and linters.
  • Help you build and maintain a cleaner architecture.
  • Play a similar role as tests in your code: they help you as a developer write better code.

Static type checkers:

  • Help catch certain errors and find bugs sooner.
  • The larger your project the more you need it: static languages offer a robustness and control that dynamic languages lack.
  • Large teams are already running static analysis.

Why should not you use type hints and static type checkers ?

  • No runtime type inference.
  • Take developer time and effort to add.
  • Add little value in short project.
  • Work best in modern Pythons.
  • Have a negative effect on the start-up time of Python programs. However, Python 3.7 introduced changes that improved start up performance (PEP-563).

7. The future of the Python type hints

Type Hinting Generics In Standard Collections

With type annotations, since Python 3.9 and the PEP-585, it is now possible to use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing.

With the following example from the Python documentation :

The code becomes :

This modification in the import of type hints makes a large part of the typing library obsolete.

Will type hints be used to do low-level optimizations?

Several ongoing projects aim to use type hints to compile Python modules to C extensions in order to generate fast code. For example :

  • Mypyc uses standard Python type hints to generate fast code. It uses ahead-of-time compilation, so compilation does not slow down program startup. Slow program startup is a common issue with JIT compilers.
  • Nuitkatranslates Python into a C program. In the future, Nuitka will be able to use a hints module to inform it about type information in order to perform as many calculations as possible in C.

Conclusion

Even though typing is an optional feature in Python, it was introduced to take advantage of some of the benefits of static typing.
In small projects, you won’t need to use it. However, it is useful when you developing large projects, as it provides the additional robustness, control, documentation and debugging capabilities needed for their successful development. But be careful, to take advantage of typing, it is necessary to use the latest available versions of Python.

Does typing make Python faster?

According to the non-goals in the PEP 484 documentation, type checking and performance optimization is dependent on third-party tools or left to the programmer. So in short: no, they will not cause any run-time effects, unless you explicitly make use of them.

What is Python typing used for?

Introduced since Python 3.5, Python's typing module attempts to provide a way of hinting types to help static type checkers and linters accurately predict errors.

Is Python type safe?

4.2. Python is a semi type safe language. Python is a dynamically and strongly typed language that has a high degree of type safety control built-in. But the type checking is done only at the run time. So, we can say that Python is not a 100% type safe language.

What are reasons for using type hinting?

As the code base gets larger, type hints can help to debug and prevent some dumb mistakes. If you're using an IDE like PyCharm, you'll get a warning message whenever you've used the wrong data type, provided you're using type hints.