Programming Search⌘K

Type hints and static typing

Type hints, the type checkers, generics, protocols, TypedDict, narrowing, overloads, stub files and the typing specification.

Features

  • Basic type annotations

    The basic hints are classes such as int and str, built-in generics such as list[str] and dict[str, int], unions written X | Y, X | None for a value that can be None, Callable, Any and object.

    core
  • Type aliases and the type statement

    The type statement gives a name to a type, as in type Point = tuple[float, float]. The alias can be generic and recursive, and Python evaluates its value only when the program asks for it.

    core
  • Type checkers

    A type checker such as mypy, pyright, Pyrefly or ty reads the hints and reports type errors without running the code. The checkers follow one specification, but their defaults and some rules differ.

    core
  • Type hints

    A type hint is an annotation that tells a static type checker the expected type of a name. Python does not check hints at run time. A separate tool, such as mypy, checks them before the program runs.

    core
  • Generic alias types

    At run time, list[int] is a types.GenericAlias object, made by list.__class_getitem__, and int | str is a union object. They keep their arguments, but Python does not check values against them.

    advanced
  • Generic functions and classes

    A type parameter list, as in def first[T](items: list[T]) -> T, makes a function or a class generic. A type variable can have a bound, constraints or a default. Python does not check type arguments.

    advanced
  • Literal, LiteralString and Final

    Literal["r", "w"] accepts only those values. LiteralString accepts only strings from the source code. Final and @final mark names, methods and classes that code must not change or extend.

    advanced
  • Overloads

    The @overload decorator gives one function several signatures for the checker, for example int to int and str to str. One plain definition after them does the work at run time.

    advanced
  • Protocols

    A protocol class lists the methods and attributes that a type must have. A class matches the protocol when it has them, with no base class needed. This is structural subtyping, or static duck typing.

    advanced
  • Special typing forms

    The typing module has forms with special rules: Self for the current class, Never for no value, ClassVar for class attributes, Annotated for metadata, NewType for distinct types and TypeForm.

    advanced
  • Stub files and typeshed

    A stub file has the suffix .pyi and gives the types of a module with no code. Typeshed has stubs for the standard library and many packages, and py.typed marks a package with its own types.

    advanced
  • Type narrowing

    After a test such as isinstance(x, int), the checker gives x a narrower type in that branch. TypeIs and TypeGuard make a function into such a test, and assert_never checks that all cases are covered.

    advanced
  • TypedDict

    A TypedDict gives a type to a dictionary with fixed string keys, where each key has its own value type. At run time, the value is a plain dict, and Python checks nothing.

    advanced
  • Variance in Python

    Variance tells whether Box[Circle] is a subtype of Box[Shape]. list and dict are invariant, Sequence and Mapping are covariant, and the argument types of Callable are contravariant.

    advanced
  • dataclass_transform and typing_extensions

    @dataclass_transform tells the checker that a library decorator, base class or metaclass makes classes like @dataclass does. typing_extensions gives new typing features to older Python versions.

    advanced
  • ParamSpec and TypeVarTuple

    A ParamSpec stands for all the parameters of a callable, so a decorator can keep the signature of the function it wraps. A TypeVarTuple stands for any number of types.

    research
  • The typing specification

    The typing specification at typing.python.org describes the Python type system for all checkers. The Typing Council of PEP 729 maintains it, and a conformance suite tests each checker against it.

    research