Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d2bd511

Browse filesBrowse files
JelleZijlstracsm10495
authored andcommitted
Enhance TypedDict docs around required/optional keys (python#109547)
As discussed in comments to python#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 e5576c0 commit d2bd511
Copy full SHA for d2bd511

File tree

1 file changed

+15
-0
lines changed
Filter options

1 file changed

+15
-0
lines changed

‎Doc/library/typing.rst

Copy file name to clipboardExpand all lines: Doc/library/typing.rst
+15Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.