mypy command is the main entry point for type checking Python code. It analyzes your code statically and reports type errors without running the code.
Basic usage
Specifying what to check
Paths to files or directories to type check. Directories are checked recursively.
Type-check the provided module. This flag may be repeated multiple times.Mypy will not recursively type check submodules.
Type-check the provided package recursively. This flag may be repeated.Mypy will recursively type check all submodules of the provided package.
Type-check the provided string as a program.
General options
Show help message and exit.
More verbose messages. Can be repeated for increased verbosity.
Show program’s version number and exit.
Set a custom output format. Choices:
jsonDefault: text outputConfig file
Configuration file with
[mypy] section.Default: mypy.ini, .mypy.ini, pyproject.toml, or setup.cfg in current directoryWarn about unused
[mypy-<pattern>] or [[tool.mypy.overrides]] config sections.Import discovery
Silently ignore imports of missing modules.Default:
falseTypecheck modules without stubs or py.typed marker.Default:
falseHow to treat imports.Choices:
normal, silent, skip, errorDefault: normalPython executable used for finding PEP 561 compliant packages and stubs.
Do not search for installed PEP 561 compliant packages.Default:
falseDisable support for namespace packages (PEP 420, init.py-less).Default:
false (namespace packages enabled)Platform configuration
Type check code assuming it will be running on Python X.Y.Example:
--python-version 3.11Type check special-cased code for the given OS platform.Default:
sys.platformAdditional variable to be considered True. May be repeated.
Additional variable to be considered False. May be repeated.
Disallow dynamic typing
Disallow Any types resulting from unfollowed imports.Default:
falseDisallow all expressions that have type Any.Default:
falseDisallow functions that have Any in their signature after decorator transformation.Default:
falseDisallow explicit Any in type positions.Default:
falseDisallow usage of generic types that do not specify explicit type parameters.Default:
falseDisallow subclassing values of type ‘Any’ when defining classes.Default:
falseUntyped definitions and calls
Disallow calling functions without type annotations from functions with type annotations.Default:
falseDisable —disallow-untyped-calls for functions/methods from specific package, module, or class. May be repeated.
Disallow defining functions without type annotations or with incomplete type annotations.Default:
falseDisallow defining functions with incomplete type annotations.Default:
falseType check the interior of functions without type annotations.Default:
falseDisallow decorating typed functions with untyped decorators.Default:
falseNone and optional handling
Assume arguments with default values of None are Optional.Default:
falseDisable strict Optional checks.Default:
false (strict Optional enabled)Configuring warnings
Warn about casting an expression to its inferred type.Default:
falseWarn about unneeded ’# type: ignore’ comments.Default:
falseDo not warn about functions that end without returning.Default:
false (warnings enabled)Warn about returning values of type Any from non-Any typed functions.Default:
falseWarn about statements or expressions inferred to be unreachable.Default:
falseStrictness flags
Strict mode. Enables all strict flags: —warn-unused-configs, —disallow-any-generics, —disallow-subclassing-any, —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-bytes, —extra-checks.Default:
falseAllow restricted variable redefinition with a new type.Default:
falseTreat imports as private unless aliased.Default:
falseProhibit equality, identity, and container checks for non-overlapping types.Default:
falseDisable treating bytearray and memoryview as subtypes of bytes.Default:
falseEnable additional checks that are technically correct but may be impractical.Default:
falseDisable a specific error code. May be repeated.
Enable a specific error code. May be repeated.
Error messages
Precede errors with “note:” messages explaining context.Default:
falseShow column numbers in error messages.Default:
falseShow end line/end column numbers in error messages. Implies —show-column-numbers.Default:
falseHide error codes in error messages.Default:
falseShow links to error code documentation.Default:
falseUse visually nicer output: soft word wrap, show source code snippets, and show error location markers.Default:
falseDo not colorize error messages.Default:
false (color enabled)Do not show error stats summary.Default:
false (summary shown)Show absolute paths to files.Default:
falseIncremental mode
Disable module cache.Default:
false (incremental mode enabled)Store module cache info in the given folder.Default:
.mypy_cacheUse a sqlite database to store the cache.Default:
falseInclude fine-grained dependency information in the cache for the mypy daemon.Default:
falseAllow using cache written by older mypy version.Default:
falseAdvanced options
Invoke pdb on fatal error.Default:
falseShow traceback on fatal error.Default:
falseRaise exception on fatal error.Default:
falseUse a custom typing module.
Use the custom typeshed in DIR.
Warn if missing type annotation in typeshed.Default:
falseWhen encountering SOURCE_FILE, read and type check the contents of SHADOW_FILE instead.
Report generation
Generate a text file report documenting expressions of type Any.
Generate a Cobertura XML type checking coverage report.
Generate an HTML type checking coverage report.
Generate a text file report documenting typed and untyped functions and lines.
Generate a JSON file mapping each source file to typed line numbers.
Generate a text file type checking coverage report.
Generate an XML type checking coverage report.
Running code
Use current directory and MYPYPATH to determine module names of files passed.Default:
falseRegular expression to match file names, directory names or paths to ignore. May be repeated.
Use .gitignore file(s) to exclude files from checking.Default:
falseMiscellaneous
Install detected missing library stub packages using pip.Default:
falseInstall stubs without asking for confirmation and hide errors (with —install-types).Default:
falseWrite a JUnit XML test result document with type checking results to FILE.
Specifies JUnit XML format:
global (single test with all errors) or per_file (one test per file).Default: globalScript x becomes module x instead of main.Default:
falseExamples
Environment variables
MYPYPATH: Additional module search path entries (colon-separated).MYPY_CACHE_DIR: Override configuration cache_dir path.
Exit codes
0: Success, no type errors found1: Type errors found2: Fatal error or mypy encountered an issue