Installing and running mypy
Mypy requires Python 3.5 or later to run. Once you’veinstalled 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 printout any errors it finds. Mypy will type check your code statically: thismeans that it will check for errors without ever running your code, justlike a linter.
This means that you are always free to ignore the errors mypy reports andtreat them as just warnings, if you so wish: mypy runs independently fromPython itself.
However, if you try directly running mypy on your existing Python code, itwill most likely report little to no errors: you must add _type annotations_to your code to take full advantage of mypy. See the section below for details.
Note
Although you must install Python 3 to run mypy, mypy is fully capable oftype checking Python 2 code as well: just pass in the —py2
flag. SeeType checking Python 2 code for more details.
- $ mypy --py2 program.py