Mypy: Optional Static Typing for Python
Mypy is a static type checker for Python, enabling optional static typing to improve code quality and maintainability. As of version 1.19.1, it supports Python 3.9 and later, with regular updates and a stable release cadence.
Warnings
- breaking Mypy 1.9 introduced breaking changes due to an updated typeshed version, dropping support for Python 3.7.
- gotcha Using 'Final' and 'TypedDict' from 'typing' requires Python 3.8 or later; for earlier versions, install 'typing_extensions'.
Install
-
pip install mypy
Imports
- Final
from typing import Final
- TypedDict
from typing import TypedDict
Quickstart
import sys
from mypy import api
result = api.run(sys.argv[1:])
if result[0]:
print('\nType checking report:\n')
print(result[0]) # stdout
if result[1]:
print('\nError report:\n')
print(result[1]) # stderr
sys.exit(result[2]) # exit status