Sub topic 16
Rolling out type checking in an existing codebase
This chapter assumes you can run mypy or Pyright and read their output. It also assumes you know what Any, # type: ignore and strict mode do. A real project may have thousands of unannotated functions, and turning on strict mode there produces thousands of errors that nobody will fix in one sitting. The way through is to make the checker pass today and then tighten it in small steps.
Start with a passing baseline
The first goal is a green run, even if the check is weak. A checker that always fails gets ignored. A checker that passes can be made stricter later.
- Install the checker and run it with default settings. Both tools skip the bodies of unannotated functions by default (mypy) or infer types from them without strict rules (Pyright).
- Fix the cheap errors, such as wrong
Nonehandling and calls with the wrong argument types. These are usually real bugs. - Silence the remaining errors in a controlled way. Use targeted
# type: ignore[code]comments or per-module settings, as described below. - Add the check to CI so that new errors cannot be introduced.
Tighten one area at a time
Apply strict rules only to the modules that are ready, and relax them for legacy code. Write the settings in pyproject.toml:
[tool.mypy]
python_version = "3.12"
files = ["src"]
warn_unused_ignores = true
[[tool.mypy.overrides]]
module = ["myapp.billing.*", "myapp.auth.*"]
disallow_untyped_defs = true
disallow_any_generics = true
warn_return_any = true
[[tool.mypy.overrides]]
module = ["myapp.legacy.*"]
ignore_errors = true
[tool.pyright]
pythonVersion = "3.12"
include = ["src"]
typeCheckingMode = "basic"
strict = ["src/myapp/billing", "src/myapp/auth"]
ignore = ["src/myapp/legacy"]mypy does not allow strict = true inside an override, so the override lists the individual flags that strict turns on. Pyright's strict setting takes a list of paths and applies strict mode to them. Its ignore setting hides diagnostics for a path but still analyzes those files for types that other modules import.
warn_unused_ignores makes mypy report any # type: ignore comment that no longer suppresses an error. Without it, stale ignores pile up after you fix the underlying problems.
Choose where to annotate first
Annotate in this order, since each step gives the most checking for the least work:
- Public functions at module boundaries, because their types spread to every caller.
- New code. Require annotations in code review so the untyped area stops growing.
- Code that is already well tested, since it is the safest to change.
- Modules that handle money, authentication or data parsing, where a type error costs the most.
Missing stubs for third-party packages are a common cause of noise. Install the types-* packages from PyPI where they exist. For the rest, add a narrow override for that package instead of a global ignore_missing_imports.
Exercise
Your project has a legacy package with hundreds of errors and a new payments package that is fully annotated. How do you configure Pyright and mypy so that payments is checked strictly without blocking CI on legacy?
Show answer
In Pyright, put src/myapp/payments in the strict list and src/myapp/legacy in ignore. In mypy, add an override for myapp.payments.* that enables flags such as disallow_untyped_defs, and an override for myapp.legacy.* with ignore_errors = true. Turn on warn_unused_ignores so stale ignore comments are reported.