mypy
mypy is a static type checker for Python that validates code against type hints and supports gradual typing.
Installation and Setup
Install it from the Python Package Index (PyPI) into a virtual environment:
Configuration can live in mypy.ini, setup.cfg, or pyproject.toml. Here’s an example:
pyproject.toml
[tool.mypy]
python_version = "3.14"
disallow_untyped_defs = true
warn_unused_ignores = true
no_implicit_optional = true
Key Features
- Checks code against type hints to catch type errors before runtime.
- Offers strictness flags and presets that encourage full annotation and tighter checks.
- Provides a file-based cache.
- Supports plugins and built-in knowledge for common libraries and patterns, such as data classes.
- Uses standard-library and third-party type stubs and recognizes
# type: ignoredirectives with optional error codes.
Usage
Run a check on a package, or path:
$ python -m mypy src/ tests/
Enable a stricter policy for a one time run:
$ python -m mypy --strict src/
Target a specific Python version when checking:
$ python -m mypy --python-version 3.14 .
Related Resources
Tutorial
Python Type Checking (Guide)
In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit way. Recent versions of Python allow you to specify explicit type hints that can be used by different tools to help you develop your code more efficiently.
For additional information on related topics, take a look at the following resources:
- Python Protocols: Leveraging Structural Subtyping (Tutorial)
- Python Type Checking (Course)
- Python Type Checking (Quiz)
- Exploring Protocols in Python (Course)
- Python Protocols: Leveraging Structural Subtyping (Quiz)
By Leodanis Pozo Ramos • Updated Dec. 12, 2025