First steps
Type system reference
Configuring and running mypy
Miscellaneous
Project Links
Mypy is a static type checker for Python.
Type checkers help ensure that you’re using variables and functions in your codecorrectly. With mypy, add type hints (PEP 484)to your Python programs, and mypy will warn you when you use those typesincorrectly.
Python is a dynamic language, so usually you’ll only see errors in your codewhen you attempt to run it. Mypy is astatic checker, so it finds bugsin your programs without even running them!
Here is a small example to whet your appetite:
number=input("What is your favourite number?")print("It is",number+1)# error: Unsupported operand types for + ("str" and "int")
Adding type hints for mypy does not interfere with the way your program wouldotherwise run. Think of type hints as similar to comments! You can always usethe Python interpreter to run your code, even if mypy reports errors.
Mypy is designed with gradual typing in mind. This means you can add typehints to your code base slowly and that you can always fall back to dynamictyping when static typing is not convenient.
Mypy has a powerful and easy-to-use type system, supporting features such astype inference, generics, callable types, tuple types, union types,structural subtyping and more. Using mypy will make your programs easier tounderstand, debug, and maintain.
Note
Although mypy is production ready, there may be occasional changesthat break backward compatibility. The mypy development team tries tominimize the impact of changes to user code. In case of a major breakingchange, mypy’s major version will be bumped.
Type system reference
Configuring and running mypy
mypy.ini
pyproject.toml
Miscellaneous
Self
TypeIs
narrows types [narrowed-type-not-subtype]#type:ignore
include an error code [ignore-without-code]#type:ignore
comment is used [unused-ignore]@override
is used when overriding a base class method [explicit-override]reveal_type
is imported from typing or typing_extensions [unimported-reveal]