PyDocs

Nav

Development Tools

TypedDict

typing

class · Added in 3.8 (source)
  • TypedDict(dict)
A simple typed namespace. At runtime it is equivalent to a plain dict.

TypedDict creates a dictionary type such that a type checker will expect all
instances to have a certain set of keys, where each key is
associated with a value of a consistent type. This expectation
is not checked at runtime.

Usage::

    >>> class Point2D(TypedDict):
    ...     x: int
    ...     y: int
    ...     label: str
    ...
    >>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'}  # OK
    >>> b: Point2D = {'z': 3, 'label': 'bad'}           # Fails type check
    >>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
    True

The type info can be accessed by calling annotationlib.get_annotations(Point2D), and
via the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
TypedDict supports an additional equivalent form::

    Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})

By default, all keys must be present in a TypedDict. It is possible
to override this by using the NotRequired and Required special forms::

    class Point2D(TypedDict):
        x: int               # the "x" key must always be present (Required is the default)
        y: NotRequired[int]  # the "y" key can be omitted

This means that a Point2D TypedDict can have the "y" key omitted, but the "x" key must be present.
Items are required by default, so the Required special form is not necessary in this example.
In addition, the total argument to the TypedDict function can be used to make all items not required::

    class Point2D(TypedDict, total=False):
        x: int
        y: int

This means that a Point2D TypedDict can have any of the keys omitted. A type
checker is only expected to support a literal False or True as the value of
the total argument. True is the default, and makes all items defined in the
class body be required. The Required special form can be used to mark individual
keys as required in a total=False TypedDict.

The ReadOnly special form can be used
to mark individual keys as immutable for type checkers::

    class DatabaseUser(TypedDict):
        id: ReadOnly[int]  # the "id" key must not be modified
        username: str      # the "username" key can be changed

The closed argument controls whether the TypedDict allows additional
non-required items during inheritance and assignability checks.
If closed=True, the TypedDict does not allow additional items::

    Point2D = TypedDict('Point2D', {'x': int, 'y': int}, closed=True)
    class Point3D(Point2D):
        z: int  # Type checker error

Passing closed=False explicitly requests TypedDict's default open behavior.
If closed is not provided, the behavior is inherited from the superclass.
A type checker is only expected to support a literal False or True as the
value of the closed argument.

The extra_items argument can instead be used to specify the assignable type
of unknown non-required keys::

    Point2D = TypedDict('Point2D', {'x': int, 'y': int}, extra_items=int)
    class Point3D(Point2D):
        z: int      # OK
        label: str  # Type checker error

The extra_items argument is also inherited through subclassing. It is unset
by default, and it may not be used with the closed argument at the same
time.

See PEPs 589, 655, 705, and 728 for more information.
Changed in 3.9, 3.11, 3.13, 3.15
  • 3.9: TypedDict is now a function rather than a class. It can still be used as a class base, as described above.
  • 3.11: Added support for marking individual keys as Required or NotRequired. See PEP 655.
  • 3.11: Added support for generic TypedDicts.
  • 3.13: Removed support for the keyword-argument method of creating TypedDicts.
  • 3.13: Support for the ReadOnly qualifier was added. See PEP 705.
  • 3.15: Support for the closed and extra_items class arguments was added. See PEP 728.
Members of TypedDict

_

__closed__
The value of the closed class argument.
__extra_items__
The value of the extra_items class argument.
__mutable_keys__
A frozenset containing the names of all mutable keys.
__optional_keys__
Point2D.__required_keys__ and Point2D.__optional_keys__ return frozenset objects containing required and non-required keys, respectively.
__readonly_keys__
A frozenset containing the names of all read-only keys.
__required_keys__
Added in version 3.9.
__total__
Point2D.__total__ gives the value of the total argument.
0 Examples
No examples for typing/TypedDict.
See Also
No see-alsos for typing/TypedDict
0 Notes
No notes for TypedDict