Skip to main content
Mypy can be configured using command-line flags in addition to configuration files. Command-line flags have higher precedence than configuration file options (except for inline configuration).

Basic usage

mypy program.py
mypy package/
mypy -m mymodule
mypy -p mypackage

Specifying code to check

files
positional arguments
Files, directories, modules, or packages to type check.
mypy file1.py file2.py
mypy src/
-m MODULE
string
Type check a module by name.
mypy -m myapp.main
-p PACKAGE
string
Type check a package and all its submodules.
mypy -p mypackage
-c PROGRAM_TEXT
string
Type check a program passed as a string.
mypy -c "print('Hello, typed world!')"

Configuration file

--config-file CONFIG_FILE
string
Specify a configuration file to use.
mypy --config-file mypy-strict.ini src/
This flag has the highest precedence and must point to a valid configuration file.
--warn-unused-configs
boolean
Warn about unused [mypy-<pattern>] config file sections.
mypy --warn-unused-configs src/

Python version and platform

--python-version VERSION
string
default:"Current Python version"
Specify the Python version to target. Format: MAJOR.MINOR.
mypy --python-version 3.10 src/
Use this to check code compatibility with different Python versions.
--platform PLATFORM
string
default:"Current platform"
Specify the platform to check against (e.g., linux, darwin, win32).
mypy --platform win32 src/
--always-true NAME
string
Variables to treat as compile-time constants that are always true.
mypy --always-true DEBUG src/
--always-false NAME
string
Variables to treat as compile-time constants that are always false.
mypy --always-false TYPING_ONLY src/

Import discovery

--ignore-missing-imports
boolean
Suppress error messages about imports that cannot be resolved.
mypy --ignore-missing-imports src/
This is a blunt instrument. Consider using per-module configuration instead.
--follow-imports {normal,silent,skip,error}
choice
default:"normal"
How to handle imports:
  • normal: Follow imports and type check them
  • silent: Follow imports but suppress errors
  • skip: Don’t follow imports (treat as Any)
  • error: Treat imports as errors
mypy --follow-imports skip src/
--python-executable EXECUTABLE
string
Python executable to use for finding PEP 561 packages.
mypy --python-executable /usr/bin/python3.11 src/
--no-site-packages
boolean
Do not search for installed PEP 561 packages.
mypy --no-site-packages src/
--no-silence-site-packages
boolean
Report errors in installed packages.
mypy --no-silence-site-packages src/

Disallow dynamic typing

--disallow-any-unimported
boolean
Disallow Any types from unfollowed imports.
mypy --disallow-any-unimported src/
--disallow-any-expr
boolean
Disallow all expressions that have type Any.
mypy --disallow-any-expr src/
--disallow-any-decorated
boolean
Disallow functions with Any in signature after decorator transformation.
mypy --disallow-any-decorated src/
--disallow-any-explicit
boolean
Disallow explicit Any in type positions.
mypy --disallow-any-explicit src/
--disallow-any-generics
boolean
Disallow generic types without explicit type parameters.
mypy --disallow-any-generics src/
--disallow-subclassing-any
boolean
Disallow subclassing values of type Any.
mypy --disallow-subclassing-any src/

Untyped definitions and calls

--disallow-untyped-calls
boolean
Disallow calling untyped functions from typed functions.
mypy --disallow-untyped-calls src/
--untyped-calls-exclude MODULE
string
Exclude specific packages/modules from --disallow-untyped-calls.
mypy --disallow-untyped-calls --untyped-calls-exclude=third_party src/
--disallow-untyped-defs
boolean
Disallow defining functions without type annotations.
mypy --disallow-untyped-defs src/
--disallow-incomplete-defs
boolean
Disallow defining functions with incomplete type annotations.
mypy --disallow-incomplete-defs src/
--check-untyped-defs
boolean
Type check the interior of functions without type annotations.
mypy --check-untyped-defs src/
--disallow-untyped-decorators
boolean
Disallow decorating typed functions with untyped decorators.
mypy --disallow-untyped-decorators src/

None and Optional handling

--strict-optional
boolean
default:"True"
Enable strict checking of optional types and None values.
mypy --strict-optional src/
--no-strict-optional
boolean
Disable strict optional checking.
mypy --no-strict-optional src/
Not recommended. This makes None compatible with all types.
--implicit-optional
boolean
Treat arguments with None default as having optional type.
mypy --implicit-optional src/

Warnings

--warn-redundant-casts
boolean
Warn about redundant casts.
mypy --warn-redundant-casts src/
--warn-unused-ignores
boolean
Warn about unneeded # type: ignore comments.
mypy --warn-unused-ignores src/
--no-warn-no-return
boolean
Don’t warn about missing return statements.
mypy --no-warn-no-return src/
--warn-return-any
boolean
Warn when returning Any from non-Any function.
mypy --warn-return-any src/
--warn-unreachable
boolean
Warn about unreachable code.
mypy --warn-unreachable src/

Strictness flags

--strict
boolean
Enable all optional error checking flags.
mypy --strict src/
This is equivalent to enabling: --disallow-any-generics, --disallow-untyped-calls, --disallow-untyped-defs, --disallow-incomplete-defs, --check-untyped-defs, --disallow-untyped-decorators, --warn-redundant-casts, --warn-unused-ignores, --warn-return-any, --no-implicit-reexport, --strict-equality, --strict-concatenate, --extra-checks
--allow-untyped-globals
boolean
Suppress errors caused by not being able to infer types of global/class variables.
mypy --allow-untyped-globals src/
--allow-redefinition
boolean
Allow variable redefinition with arbitrary types.
mypy --allow-redefinition src/
--local-partial-types
boolean
Disallow partial types spanning module top level and function.
mypy --local-partial-types src/
--no-implicit-reexport
boolean
Require explicit re-export using from ... as ... or __all__.
mypy --no-implicit-reexport src/
--strict-equality
boolean
Prohibit equality/identity/container checks between non-overlapping types.
mypy --strict-equality src/
--extra-checks
boolean
Enable additional checks that are technically correct but impractical.
mypy --extra-checks src/

Error codes

--enable-error-code ERROR_CODE
string
Enable specific error codes.
mypy --enable-error-code truthy-bool,ignore-without-code src/
--disable-error-code ERROR_CODE
string
Disable specific error codes.
mypy --disable-error-code misc,no-untyped-def src/

Error message formatting

--show-error-context
boolean
Show context in error messages.
mypy --show-error-context src/
--show-column-numbers
boolean
Show column numbers in error messages.
mypy --show-column-numbers src/
Show documentation links for error codes.
mypy --show-error-code-links src/
--hide-error-codes
boolean
Hide error codes in error messages.
mypy --hide-error-codes src/
--pretty
boolean
Use visually nicer output with soft word wrap and source snippets.
mypy --pretty src/
--no-color-output
boolean
Disable color in error messages.
mypy --no-color-output src/
--no-error-summary
boolean
Hide error summary.
mypy --no-error-summary src/
--show-absolute-path
boolean
Show absolute paths in error messages.
mypy --show-absolute-path src/

Incremental mode

--no-incremental
boolean
Disable incremental mode.
mypy --no-incremental src/
--cache-dir DIR
string
default:".mypy_cache"
Store cache info in the specified directory.
mypy --cache-dir /tmp/mypy_cache src/
--sqlite-cache
boolean
Use SQLite database for cache storage.
mypy --sqlite-cache src/
--cache-fine-grained
boolean
Include fine-grained dependencies in cache.
mypy --cache-fine-grained src/
--skip-version-check
boolean
Use cache even if generated by different Mypy version.
mypy --skip-version-check src/
--skip-cache-mtime-checks
boolean
Skip cache internal consistency checks based on mtime.
mypy --skip-cache-mtime-checks src/

Reports

Report generation disables incremental mode and slows down checking.
--any-exprs-report DIR
string
Generate a report of Any expressions.
mypy --any-exprs-report reports/ src/
--html-report DIR
string
Generate an HTML coverage report.
mypy --html-report htmlcov/ src/
--linecount-report DIR
string
Generate a line count report.
mypy --linecount-report reports/ src/
--linecoverage-report DIR
string
Generate a JSON line coverage report.
mypy --linecoverage-report reports/ src/

Advanced options

--plugins PLUGIN
string
Specify Mypy plugins to use.
mypy --plugins pydantic.mypy src/
--exclude PATTERN
string
Exclude files/directories matching the pattern.
mypy --exclude '/tests/' src/
--verbose
boolean
Show more verbose messages.
mypy --verbose src/
--pdb
boolean
Invoke debugger on fatal error.
mypy --pdb src/
--show-traceback
boolean
Show traceback on fatal error.
mypy --show-traceback src/
--raise-exceptions
boolean
Raise exceptions on fatal errors (for debugging).
mypy --raise-exceptions src/

Output options

--junit-xml FILE
string
Write a JUnit XML file with type checking results.
mypy --junit-xml junit.xml src/
--junit-format {global,per_file}
choice
default:"global"
Format for JUnit XML output.
mypy --junit-xml junit.xml --junit-format per_file src/

Combining flags

mypy --strict \
  --show-error-codes \
  --pretty \
  src/

Build docs developers (and LLMs) love