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.
Common errors
-
Cannot find implementation or library stub for module named 'foo'
cause Mypy cannot locate the Python module 'foo' or its associated type stubs (.pyi files), often because it's a third-party library without stubs, a local module not on Mypy's search path, or not installed in the environment Mypy is checking.fixInstall the module (e.g., `pip install foo`), install its type stubs (e.g., `pip install types-foo` or `mypy --install-types`), add the module's parent directory to `MYPYPATH`, or configure `mypy.ini` with `ignore_missing_imports = True` for the specific module (e.g., `[mypy-foo.*] ignore_missing_imports = True`). -
Incompatible types in assignment (expression has type "X", variable has type "Y")
cause You are attempting to assign a value of one type (expression type 'X') to a variable that has been explicitly or implicitly typed as another incompatible type ('Y').fixEnsure the assigned value's type matches the variable's expected type, or explicitly annotate the variable with a `Union` type if it can legitimately hold different types, or use `cast()` if you are certain about the type and want to override Mypy. -
Need type annotation for 'variable_name'
cause Mypy is unable to infer the precise type of a variable, typically occurring with empty collections (e.g., `[]`, `{}`) or when `--disallow-untyped-defs` or `warn_incomplete_defs` is enabled, requiring an explicit type hint.fixAdd an explicit type annotation to the variable, especially for empty containers (e.g., `my_list: list[str] = []` or `my_dict: dict[str, int] = {}`). -
Name 'name' is not defined
cause Mypy detects a reference to a name (variable, function, class, or module) that has not been defined or properly imported within the current scope. This often indicates a typo or a missing import.fixCorrect any typos in the name, ensure the item is imported from the correct module, or define the name before its first use. -
Function does not return a value
cause A function or method is annotated with a return type other than `None` (or implicitly expected to return a value), but there is a code path where it implicitly returns `None` (e.g., by reaching the end of the function without an explicit `return` statement).fixEnsure all code paths within the function explicitly return a value consistent with its return type annotation, or change the return type annotation to `None` if the function is not meant to return anything.
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'.
- gotcha Mypy reported a 'Missing target' error, indicating that it was called without specifying any files, modules, or packages to type-check. This is a usage error.
- breaking Mypy failed because no target module, package, or files were specified. The 'mypy' command requires at least one argument indicating what to type-check.
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