Configuration file discovery
By default, Mypy discovers configuration files by walking up the file system. In each directory, it looks for these files (in order):mypy.ini.mypy.inipyproject.toml(containing a[tool.mypy]section)setup.cfg(containing a[mypy]section)
$XDG_CONFIG_HOME/mypy/config~/.config/mypy/config~/.mypy.ini
The
--config-file command-line flag has the highest precedence and must point to a valid configuration file.Configuration file format
Mypy configuration files use the INI file format with sections in square brackets and settings asNAME = VALUE pairs. Comments start with #.
Basic structure
Global section
A section named[mypy] must be present and specifies global flags.
Specifies the Python version used to parse and check the target program. Format: This option may only be set in the global section.
MAJOR.MINOR (e.g., 3.10)Specifies paths to use for module discovery, after trying Relative paths are treated relative to the working directory. Use
MYPYPATH environment variable. Multiple paths are separated with : or ,. User home directory and environment variables will be expanded.MYPY_CONFIG_FILE_DIR to refer to paths relative to the config file.This option may only be set in the global section.A comma-separated list of paths to check if none are given on the command line. Supports recursive file globbing using This option may only be set in the global section.
glob.A regular expression matching file names, directory names, and paths to ignore while recursively discovering files.This option may only be set in the global section.
Enable all optional error checking flags. The exact list of flags may change over time.
Per-module sections
Additional sections named[mypy-PATTERN1,PATTERN2,...] specify flags that apply only to modules whose name matches at least one pattern.
Pattern syntax
Concrete module names
Concrete module names
A pattern like
foo.bar matches only the named module.Structured wildcards
Structured wildcards
A pattern like
foo.bar.* matches foo.bar and any submodules (e.g., foo.bar.baz, foo.bar.baz.quux).Unstructured wildcards
Unstructured wildcards
Stars may appear in the middle of a name (e.g.,
site.*.migrations.*). Stars match zero or more module components.Configuration precedence
When options conflict, the precedence order is:- Inline configuration in the source file
- Sections with concrete module names (
foo.bar) - Sections with unstructured wildcard patterns (
foo.*.baz), with sections later in the file overriding earlier sections - Sections with structured wildcard patterns (
foo.bar.*), with more specific overriding more general - Command line options
- Top-level configuration file options
Common configuration options
Type checking strictness
Disallow defining functions without type annotations or with incomplete type annotations.
Disallow usage of generic types that do not specify explicit type parameters.
Disallow calling functions without type annotations from functions with type annotations.
Type-check the interior of functions without type annotations.
Import handling
Suppress error messages about imports that cannot be resolved.
If used in a per-module section, the module name should match the name of the imported module, not the module containing the import statement.
Directs what to do with imports when the imported module is found as a
.py file. Possible values:normal: Follow imports normallysilent: Follow imports but suppress errorsskip: Don’t follow importserror: Treat imports as errors
Warnings
Show a warning when returning a value with type
Any from a function declared with a non-Any return type.Warn about unneeded
# type: ignore comments.Warn about casting an expression to its inferred type.This option may only be set in the global section.
Warn about per-module sections in the config file that do not match any files processed.
Optional type handling
Enable strict checking of optional types and
None values. When disabled, None is treated as compatible with every type.Treat parameters with a
None default value as having an implicit optional type.Incremental mode
Enable incremental mode for faster repeat checks.This option may only be set in the global section.
Specifies the location where Mypy stores incremental cache info. User home directory and environment variables will be expanded.This option may only be set in the global section.
Using pyproject.toml
When usingpyproject.toml, some syntax differences apply:
Section naming
Section naming
The
[mypy] section becomes [tool.mypy]:Per-module overrides
Per-module overrides
Module-specific sections use
[[tool.mypy.overrides]]:TOML syntax requirements
TOML syntax requirements
- Strings must be wrapped in double quotes or single quotes
- Boolean values should be lowercase (
true,false) - Lists use array syntax:
["item1", "item2"]
Inverting option values
Options that take boolean values may be inverted by:- Adding
no_to their name (e.g.,no_strict_optional) - Swapping prefix from
disallowtoallow(e.g.,allow_untyped_defsinstead ofdisallow_untyped_defs)
Path expansion
Some options support user home directory and environment variable expansion:- Use
~at the beginning of a path for the user home directory - Use
$VARNAMEor${VARNAME}for environment variables - Use
$MYPY_CONFIG_FILE_DIRto refer to the directory containing the config file
Advanced plugins
A comma-separated list of Mypy plugins.This option may only be set in the global section.
Report generation
Generate a text file report documenting expressions of type
Any.Generate an HTML type checking coverage report. Requires
lxml library.Platform configuration
Specifies the OS platform for the target program (e.g., This option may only be set in the global section.
darwin, win32, linux).Variables that Mypy will treat as compile-time constants that are always true.
Variables that Mypy will treat as compile-time constants that are always false.