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:

Windows PowerShell
(venv) PS> py -m pip install mypy
(venv) PS> py -m mypy --version
Shell
(venv) $ python -m pip install mypy
(venv) $ python -m mypy --version

Configuration can live in mypy.ini, setup.cfg, or pyproject.toml. Here’s an example:

TOML 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: ignore directives with optional error codes.

Usage

Run a check on a package, or path:

Shell
$ python -m mypy src/ tests/

Enable a stricter policy for a one time run:

Shell
$ python -m mypy --strict src/

Target a specific Python version when checking:

Shell
$ python -m mypy --python-version 3.14 .

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.

intermediate best-practices

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Dec. 12, 2025