Skip to main content

Lib/typing.py (extensions)

Source:

cpython 3.14 @ ab2d84fe1023/Lib/typing.py

This annotation covers the advanced typing module constructs added in Python 3.8-3.12. See lib_typing_detail for the core generics (Generic, Union, Optional, Callable).

Map

LinesSymbolRole
1-200Protocol, runtime_checkableStructural subtyping via __protocol_attrs__
201-400TypedDictDict subtype with per-key type annotations
401-550LiteralType that represents a fixed set of values
551-700FinalMarks a variable as immutable
701-850AnnotatedAttach metadata to a type hint
851-1000TypeVar with constraints, ParamSpecType variable extensions
1001-1150TypeVarTuple, UnpackVariadic generics (PEP 646)
1151-1250Never, NoReturn, LiteralString, SelfSpecial forms
1251-1400assert_never, assert_type, reveal_typeType checker utilities
1401-1600get_type_hints, get_overloadsRuntime introspection

Reading

Protocol

Protocol enables structural (duck-type) subtyping. A class is a virtual subclass of a Protocol if it implements all the protocol's abstract methods, even without explicit inheritance.

# CPython: Lib/typing.py:1845 Protocol.__init_subclass__
class Protocol(metaclass=_ProtocolMeta):
...
def __init_subclass__(cls, *args, **kwargs):
...
if '__protocol_attrs__' not in cls.__dict__:
cls.__protocol_attrs__ = frozenset(
attr for attr in dir(cls)
if not attr.startswith('_') or attr.startswith('__') and attr.endswith('__')
)

@runtime_checkable enables isinstance(obj, MyProtocol) checks at runtime.

TypedDict

# CPython: Lib/typing.py:2240 TypedDict
class Movie(TypedDict):
name: str
year: int

TypedDict creates a dict subtype with per-key annotations. At runtime, it is a plain dict; the annotations are used only by type checkers.

Literal

# CPython: Lib/typing.py:490 Literal
Status = Literal['pending', 'active', 'closed']

Literal restricts a value to a set of specific values. The __args__ tuple stores the allowed values.

Annotated

# CPython: Lib/typing.py:730 Annotated
MaxLen100 = Annotated[str, MaxLength(100)]

Annotated[T, metadata...] wraps a type with arbitrary metadata. get_type_hints(func, include_extras=True) preserves the metadata; without include_extras, the Annotated wrapper is stripped.

ParamSpec

Captures the parameter specification of a callable, enabling typed decorators:

# CPython: Lib/typing.py:2490 ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
def decorator(func: Callable[P, T]) -> Callable[P, T]: ...

gopy notes

typing is used extensively in Python 3.10+ code. gopy needs get_type_hints() for runtime annotation inspection (used by dataclasses, attrs, etc.). Protocol.__subclasshook__ for runtime_checkable requires gopy's isinstance to call the hook. TypeVar, Union, and Optional are needed for dataclasses field type introspection.