Pytest and mypy
mypy provides (optional) type-checking for Python.
You can annotate your code with type information just like in the strongly typed languages like C or Java. Python will happily let you do that and promptly disregard them.
However they will help you reading the code. They will help your IDE and you can check them using mypy.
It is recommended that you configure mypy to run in both the CI and in the pre-commit hook.
Install
$ pip install mypy
$ pip install pytest-mypy
Run mypy
$ mypy mymod.py
$ pytest --mypy
Sample code
import sys
z = "23"
z = int(z)
def add(x: int, y):
return x + y
Regular test
import mymod
x = "23"
x = int(x)
def test_add():
assert mymod.add(2, 3) == 5
Run the test
pytest
Run mypy
$ mypy mymod.py
mymod.py:4: error: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment]
Found 1 error in 1 file (checked 1 source file)
Run mypy with pytest
$ pytest --mypy
mymod.py FF
test_mymod.py F.
Excluding files when using mypy works, but that does not exclude them when using pytest --mypy
[mypy]
exclude=test_mymod.py
Not even this:
[mypy]
exclude=test_mymod.py