Skip to content

Enhance TypedDict docs around required/optional keys #109547

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
Sep 27, 2023
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
15 changes: 15 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,13 @@ types.
>>> Point3D.__total__
True

This attribute reflects *only* the value of the ``total`` argument
to the current ``TypedDict`` class, not whether the class is semantically
total. For example, a ``TypedDict`` with ``__total__`` set to True may
have keys marked with :data:`NotRequired`, or it may inherit from another
``TypedDict`` with ``total=False``. Therefore, it is generally better to use
:attr:`__required_keys__` and :attr:`__optional_keys__` for introspection.

.. attribute:: __required_keys__

.. versionadded:: 3.9
Expand Down Expand Up @@ -2439,6 +2446,14 @@ types.

.. versionadded:: 3.9

.. note::

If ``from __future__ import annotations`` is used or if annotations
are given as strings, annotations are not evaluated when the
``TypedDict`` is defined. Therefore, the runtime introspection that
``__required_keys__`` and ``__optional_keys__`` rely on may not work
properly, and the values of the attributes may be incorrect.

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

.. versionadded:: 3.8
Expand Down