Checking of type hints using mypy¶
Optional type hints can be added to python code which allows specification of the type of a variable. Type hints were specified in PEP484.
Type hints are ignored when running the code but can be statically analysed using a various tools:
Mypy is used for Nashpy.
For example, consider the file main.py
:
def get_mean(collection):
"""
Obtain the average of a collection of objects.
Parameters
----------
collection : list
A list of numbers
Returns
-------
float
The mean of the numbers.
"""
return sum(collection) / len(collection)
After installing mypy:
$ python -m pip install mypy
If we check the annotations present in the file:
$ python -m mypy main.py
Success: no issues found in 1 source file
There are no issues because there are no annotations. If the following annotations are added:
from typing import Iterable
def get_mean(collection: Iterable) -> float:
"""
Obtain the average of a collection of objects.
Parameters
----------
collection : Iterable
A list of numbers
Returns
-------
float
The mean of the numbers.
"""
return sum(collection) / len(collection)
We get:
$ python -m mypy main.py
main_with_wrong_types.py:17: error: Argument 1 to "len" has incompatible type "Iterable[Any]"; expected "Sized"
Found 1 error in 1 file (checked 1 source file)
Mypy has found an error here: the Iterable
type does not necessarily
have a length. The following modifies this:
def get_mean(collection: list) -> float:
"""
Obtain the average of a collection of objects.
Parameters
----------
collection : list
A list of numbers
Returns
-------
float
The mean of the numbers.
"""
return sum(collection) / len(collection)
We get:
$ python -m mypy main.py
Success: no issues found in 1 source file
–ignore-missing-import¶
In some cases some imported modules cannot be used checked with Mypy, these can be ignored by running the following:
$ python -m mypy --ignore-missing-import main.py
Overlap of functionality with darglint¶
The python library darglint checks the format of the docstrings. This will also use any type annotations and so the type annotations and the types specified in the docstrings must correspond.