summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2023-09-27 17:35:46 (GMT)
committerGitHub <noreply@github.com>2023-09-27 17:35:46 (GMT)
commitf49958c886a2f2608f1008186d588efc2a98b445 (patch)
tree71d43e2548eb2061798bce5e11624fd902dbce08 /Doc
parent74723e11109a320e628898817ab449b3dad9ee96 (diff)
downloadcpython-f49958c886a2f2608f1008186d588efc2a98b445.zip
cpython-f49958c886a2f2608f1008186d588efc2a98b445.tar.gz
cpython-f49958c886a2f2608f1008186d588efc2a98b445.tar.bz2
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.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/typing.rst15
1 files changed, 15 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index e63b839..8f69193 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -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
@@ -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