Skip to main content Brad's PyNotes

Posts on Typing

  1. PEP 544: Protocols - Structural Subtyping (Static Duck Typing)

    TL;DR

    PEP 544 introduces Protocol classes that enable structural subtyping (static duck typing) - type checking based on what methods an object has rather than its inheritance hierarchy, making Python’s type system more flexible and duck-typing friendly.

  2. PEP 604 Union Types

    TL;DR

    PEP 604 introduced the | operator for union types, allowing int | str instead of Union[int, str] for cleaner type annotations.

    Interesting!

    The | operator for union types makes Python’s type hints look more like mathematical set notation - int | str means “integer OR string”!

  3. PEP 3107 Function Annotations

    TL;DR

    PEP 3107 introduced syntax for adding metadata to function parameters and return values, stored in __annotations__ - the foundation for modern type hints.

  4. PEP 585 Generics

    TL;DR

    PEP 585 made built-in collections generic, allowing list[int] instead of List[int] - eliminating duplicate type hierarchies in the typing module.

    Interesting!

    Before PEP 585, Python had two separate type systems - one for runtime and one for type hints. Now built-in collections work for both!

  5. PEP 526: Variable Annotations - Type Hints for Variables

    TL;DR

    PEP 526 introduced variable annotations in Python 3.6, allowing type hints for variables using the syntax variable: type = value, extending PEP 484’s function annotations to all variables for better code documentation and static analysis.