mypy(1)
Static type checker for Python that finds bugs by analyzing code without running it.
Synopsis
mypy [OPTIONS] [SOURCE_FILE_OR_DIRECTORY]...Description
mypy is a static type checker for Python. It uses type hints (annotations) to verify that your code is type-safe, catching common errors like passing the wrong type to a function or accessing nonexistent attributes before runtime.
mypy reads Python source files, analyzes type annotations and infers types from context, then reports any type mismatches or issues. It supports gradual typing, allowing you to add type hints incrementally to existing codebases.
Common options
| Flag | What it does |
|---|---|
--strict | Enable all optional error checking flags for maximum strictness |
--ignore-missing-imports | Suppress errors about missing type stubs or imports for untyped libraries |
--no-implicit-optional | Disallow implicit Optional types for arguments with None defaults |
--disallow-untyped-defs | Require type annotations on all function definitions |
--check-untyped-defs | Type-check function bodies without type annotations |
--warn-unused-ignores | Warn about unused '# type: ignore' comments |
--pretty | Pretty-print error messages with color and context |
--junit-xml FILE | Write results in JUnit XML format to specified file |
--html DIR | Generate HTML coverage reports in the specified directory |
--cache-dir DIR | Store incremental type-checking cache in this directory |
--no-incremental | Disable incremental mode; re-check all files each run |
--follow-imports silent | Follow imports but suppress errors from imported modules |
Examples
Type-check a single Python file and report any errors
mypy app.pyRecursively type-check all Python files in the src directory
mypy src/Check with strict mode enabled, enforcing comprehensive type coverage
mypy --strict mymodule.pyCheck app.py but ignore errors for untyped third-party libraries
mypy --ignore-missing-imports app.pyRequire all functions to have type annotations and type-check their bodies
mypy --disallow-untyped-defs --check-untyped-defs src/Generate an HTML coverage report showing type-checked portions of code
mypy --html report/ src/Force a full re-check of all files, ignoring the cache
mypy --no-incremental src/