mypy: Static Type Checking Command Reference
Catch type errors before runtime.
mypy statically checks Python type annotations, flagging mismatches before they reach runtime. CI runs it as a gate so type regressions fail the build.
Common flags / usage
- mypy src/ -- type-check a package or path
- --strict -- enable the full set of strict checks
- --ignore-missing-imports -- skip unresolved third-party imports
- --install-types --non-interactive -- fetch missing stub packages
- --cache-dir DIR -- control the incremental cache location
Example
shell
- run: pip install mypy
- uses: actions/cache@v4
with:
path: .mypy_cache
key: mypy-${{ hashFiles('**/*.py') }}
- run: mypy --strict src/In CI
mypy is incremental -- cache .mypy_cache across runs for a big speedup. Missing third-party stubs cause errors; install stub packages (or run --install-types --non-interactive) rather than blanket --ignore-missing-imports, which hides real issues.
Key takeaways
- mypy gates the build on static type-checking errors.
- Cache .mypy_cache to make repeated CI runs much faster.
- Install type stubs instead of broadly ignoring missing imports.
Related guides
ruff check: Fast Linting Command ReferenceReference for ruff check in CI: fast Python linting, the --fix and --output-format flags, gating on lint erro…
flake8: Style Linting Command ReferenceReference for flake8 in CI: linting for style and simple errors, selecting and ignoring codes, configuration…
pytest: Run Tests Command Reference for CIReference for pytest in CI: running the suite, useful flags, why python -m pytest fixes import errors, and a…