Skip to content

Commit f49958c

Browse files
Enhance TypedDict docs around required/optional keys (#109547)
As discussed in comments to #109544, the semantics of this attribute are somewhat confusing. Add a note explaining its limitations and steering users towards __required_keys__ and __optional_keys__ instead.
1 parent 74723e1 commit f49958c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Doc/library/typing.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,13 @@ types.
24042404
>>> Point3D.__total__
24052405
True
24062406

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

24092416
.. versionadded:: 3.9
@@ -2439,6 +2446,14 @@ types.
24392446

24402447
.. versionadded:: 3.9
24412448

2449+
.. note::
2450+
2451+
If ``from __future__ import annotations`` is used or if annotations
2452+
are given as strings, annotations are not evaluated when the
2453+
``TypedDict`` is defined. Therefore, the runtime introspection that
2454+
``__required_keys__`` and ``__optional_keys__`` rely on may not work
2455+
properly, and the values of the attributes may be incorrect.
2456+
24422457
See :pep:`589` for more examples and detailed rules of using ``TypedDict``.
24432458

24442459
.. versionadded:: 3.8

0 commit comments

Comments
 (0)