Skip to content

bpo-43345: Enhance TypedDict documentation. #24668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1247,26 +1247,28 @@ These are not used in annotations. They are building blocks for declaring types.

assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')

The type info for introspection can be accessed via ``Point2D.__annotations__``
and ``Point2D.__total__``. To allow using this feature with older versions
of Python that do not support :pep:`526`, ``TypedDict`` supports two additional
equivalent syntactic forms::
The type info for introspection can be accessed via ``Point2D.__annotations__``,
``Point2D.__total__``, ``Point2D.__required_keys__``, and
``Point2D.__optional_keys__``.
To allow using this feature with older versions of Python that do not
support :pep:`526`, ``TypedDict`` supports two additional equivalent
syntactic forms::

Point2D = TypedDict('Point2D', x=int, y=int, label=str)
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 specifying totality.
By default, all keys must be present in a ``TypedDict``. It is possible to
override this by specifying totality.
Usage::

class point2D(TypedDict, total=False):
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.
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 required.

See :pep:`589` for more examples and detailed rules of using ``TypedDict``.

Expand Down Expand Up @@ -1980,4 +1982,3 @@ Constant
(see :pep:`563`).

.. versionadded:: 3.5.2