Skip to content

Typing

This section treats typing as an API and boundary design tool, not as a side quest for pleasing a type checker. In Python 3.14, typing is more language-shaped than it used to be, and it is also more deeply connected to runtime metadata consumers such as FastAPI and Pydantic.

Quick takeaway: good typing is not about adding more annotations. It is about making public API shapes clear, making narrowing safe, and keeping the boundary between static meaning and runtime metadata explicit.

Questions This Part Answers

Which types read well?

`X | Y`, `type` aliases, and built-in generics can make APIs much more readable.

When do generics matter?

Generics keep reusable APIs from losing concrete type information.

How do you type duck typing?

`Protocol` lets you describe capabilities without forcing inheritance.

How do static and runtime views differ?

Type checkers and runtime frameworks consume annotations for different reasons and in different ways.

  1. Modern Typing
  2. Generics
  3. Protocols
  4. Type Narrowing
  5. Runtime vs Static

Practical Connections

  • FastAPI request and response contracts
  • Pydantic Annotated metadata and validation
  • SQLAlchemy repositories and service-layer APIs

Built with VitePress for a Python 3.14 handbook.