Type Hints Best Practices
Types should reduce incidents without drowning the team in ceremony. These rules focus effort where static analysis prevents real production bugs.
Search across all documentation pages
Types should reduce incidents without drowning the team in ceremony. These rules focus effort where static analysis prevents real production bugs.
Any count grows or strict overrides multiply.list[str], not bare list. Bare generics defeat checking.X | Y union syntax on Python 3.10+. Consistent with 3.14 codebase.collections.abc types in parameters (Sequence, Mapping). Accept wider read-only inputs.TypedDict/NamedTuple for JSON rows; dataclass for domain objects. Match runtime shape.Literal and StrEnum for fixed string sets. Prevent typos at check time.Any - use object when truly unknown then narrow. Track Any with lint rule where possible.types-* stubs for untyped dependencies. requests, pyyaml, etc.warn_unused_ignores and warn_return_any. Clean stale ignores promptly.ignore_errors. Quarantine with sunset dates.typing.cast to fix logic. cast silences checker only.isinstance / match narrowing after optional checks. if x is not None pattern.type: ignore unavoidable with error code and ticket link.ignore_errors must have owner and date.TypeVar) when utility preserves input types. Not Any in/out.@overload sparingly for real signature dependence. Not for cosmetic hints.Public APIs yes; trivial locals optional unless mypy needs help inferring.
Pydantic adds runtime validation; keep hints on pure functions for mypy coverage.
Lower priority than src; type tests when they clarify fixtures or prevent copy-paste errors.
Use library stubs and precise dtypes where ROI clear; do not block ship on perfect ndarray generics.
2.0 style mapped types improving - follow sqlalchemy stubs/plugins in mypy config.
Rollback override to previous level; fix forward in weekly slices not heroics.
Short ADR: checker choice, strict timeline, Pydantic boundaries, Protocol for ports.
Some teams cap Any count via script in CI - optional advanced governance.
Fine for dev; still need CI gate so teammates without Pylance get same safety.
Typed HTTP models + service function signatures - catches most production type bugs early.
Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 16, 2026