MyPy¶
Mypy is a static type checker for Python. If you sprinkle your code with type annotations, mypy can type check your code and find common bugs. As mypy is a static analyzer or a lint-like tool, the type annotations are just hints for mypy and don’t interfere when running your program. You run your program with a standard Python interpreter, and the annotations are treated effectively as comments.
For further information visit https://mypy.readthedocs.io/en/stable/introduction.html
Installation¶
$ poetry add mypy --dev
Configuration¶
Add the following in setup.cfg
:
[mypy]
ignore_missing_imports = true
disallow_untyped_defs = true
warn_redundant_casts = true
warn_unreachable = true
show_error_codes = true
pretty = true
Usage¶
$ mypy python_lifecycle_training tests
Resolution¶
from numbers import Real
def add(a: Real, b: Real) -> Real:
return a + b
def test_version() -> None:
assert __version__ == "0.1.0"
Add a badge¶
.. image:: http://www.mypy-lang.org/static/mypy_badge.svg
:target: http://mypy-lang.org/
:alt: Checked with mypy
Next Step¶
To move on to the next step commit or stash your changes then checkout to the branch
init/pre-commit
$ git stash
$ git checkout init/pre-commit
Uninstall¶
$ poetry remove mypy --dev