$linuxjunkies
>

mypy(1)

Static type checker for Python that finds bugs by analyzing code without running it.

UbuntuDebianFedoraArch

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

FlagWhat it does
--strictEnable all optional error checking flags for maximum strictness
--ignore-missing-importsSuppress errors about missing type stubs or imports for untyped libraries
--no-implicit-optionalDisallow implicit Optional types for arguments with None defaults
--disallow-untyped-defsRequire type annotations on all function definitions
--check-untyped-defsType-check function bodies without type annotations
--warn-unused-ignoresWarn about unused '# type: ignore' comments
--prettyPretty-print error messages with color and context
--junit-xml FILEWrite results in JUnit XML format to specified file
--html DIRGenerate HTML coverage reports in the specified directory
--cache-dir DIRStore incremental type-checking cache in this directory
--no-incrementalDisable incremental mode; re-check all files each run
--follow-imports silentFollow imports but suppress errors from imported modules

Examples

Type-check a single Python file and report any errors

mypy app.py

Recursively type-check all Python files in the src directory

mypy src/

Check with strict mode enabled, enforcing comprehensive type coverage

mypy --strict mymodule.py

Check app.py but ignore errors for untyped third-party libraries

mypy --ignore-missing-imports app.py

Require 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/