PEP 728: TypedDict with Typed Extra Items

The proposal has been updated mainly addressing these topics:

  • Spec out the closed=True proposal and use __extra_items__ as the special key name
  • Enhance the extra=Type discussion under the “Rejected Ideas” section
  • Discuss the interaction between closed vs. non-closed TypedDict.
  • Note on backwards compatibility between closed and the keyword arguments flavor of using the functional TypedDict alternative (i.e., TD = TypedDict("TD", foo=int, bar=str, closed=SomeType))
  • Use a more specific name "__extra_items__" for the special key.

I also want to note that support for this in typing_extensions has been merged, and we are expecting to get it released in 4.10.0. This will introduce two attributes allowing runtime introspection:

  • __closed__
    A boolean flag indicating whether the current TypedDict is
    considered closed.

    This is not inherited by the TypedDict’s
    subclasses.

  • __extra_items__
    The type annotation of the extra items allowed on the TypedDict.

    This attribute defaults to None on a TypedDict that has itself and
    all its bases non-closed. This default is different from type(None)
    that represents __extra_items__: None defined on a closed
    TypedDict.

    If __extra_items__ is not defined or inherited on a closed
    TypedDict, this defaults to Never.

3 Likes