Type Hints Highlights Summary
Every highlight bullet from the 12 pages in this section, gathered on one page and grouped by the page it came from.
- Type hints are metadata for an external checker, not runtime enforcement by the interpreter
- Gradual typing lets typed and untyped code interoperate in the same codebase
- Protocol checks shape (structural typing); class hierarchies check ancestry (nominal typing)
- A checker's model of your program can diverge from what actually runs - that gap is where bugs hide
- Hints are for static checkers, not runtime enforcement
- Annotate public APIs and complex locals
- Use list[int] syntax on Python 3.9+
- mypy/pyright catch bugs before production
- Use list[int] not List[int] on 3.9+
- Prefer collections.abc for read-only APIs
- Iterable for consume-once streams
- Mapping over dict when immutability implied
- str | None preferred over Optional[str]
- Narrow with if x is not None
- match/case narrows union members
- Avoid Optional when None invalid
- TypeVar links input and output types
- Generic classes parameterize containers
- PEP 695 type alias syntax in 3.12+
- bound= restricts TypeVar to subtype
- Protocol checks shape, not inheritance
- Ideal for third-party classes
- runtime_checkable for isinstance tests
- Combine with Generic for Box[T] protocols
- TypedDict types dict keys without runtime class
- NamedTuple immutable with named fields
- total=False for optional TypedDict keys
- Prefer Pydantic at HTTP boundaries
- Literal restricts to exact values
- Final marks constants and non-overrides
- Annotated attaches metadata for tools
- Discriminated unions use Literal tags
- @overload describes multiple call signatures
- Implementation signature must be compatible
- ParamSpec preserves decorator argument types
- Callable[[A,B], R] for simple function types
- Start permissive; tighten per package
- pyproject.toml centralizes checker config
- Run mypy in CI on every PR
- ruff does not replace mypy/pyright
- Type boundaries before inner loops
- One package per PR tranche
- Pydantic at HTTP; plain types inside
- Track mypy error count over time
- Type boundaries first - HTTP, config, public APIs
- Prefer str | None over bare Optional style drift
- Protocol ports for testability
- Pydantic 2 validates; hints document
Revisado por Chris St. John·Última actualización: 31 jul 2026