Skip to content

Commit a2d8071

Browse files
committed
pythongh-91243: Document Required and NotRequired
1 parent 37c9a35 commit a2d8071

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

Doc/library/typing.rst

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ annotations. These include:
7878
*Introducing* :data:`TypeVarTuple`
7979
* :pep:`647`: User-Defined Type Guards
8080
*Introducing* :data:`TypeGuard`
81+
* :pep:`655`: Marking individual TypedDict items as required or potentially-missing
82+
*Introducing* :data:`Required` and :data:`NotRequired`
8183
* :pep:`673`: Self type
8284
*Introducing* :data:`Self`
8385
* :pep:`675`: Arbitrary Literal String Type
@@ -1022,6 +1024,18 @@ These can be used as types in annotations using ``[]``, each having a unique syn
10221024

10231025
.. versionadded:: 3.8
10241026

1027+
.. data:: Required
1028+
1029+
.. data:: NotRequired
1030+
1031+
A special typing construct that marks individual keys of a :class:`TypedDict`
1032+
as either required or non-required respectively.
1033+
1034+
For more information, see
1035+
:pep:`655` (Marking individual TypedDict items as required or potentially-missing).
1036+
1037+
.. versionadded:: 3.11
1038+
10251039
.. data:: Annotated
10261040

10271041
A type, introduced in :pep:`593` (``Flexible function and variable
@@ -1706,7 +1720,21 @@ These are not used in annotations. They are building blocks for declaring types.
17061720
Point2D = TypedDict('Point2D', {'in': int, 'x-y': int})
17071721

17081722
By default, all keys must be present in a ``TypedDict``. It is possible to
1709-
override this by specifying totality.
1723+
mark individual keys as non-required using ``NotRequired``::
1724+
1725+
class Point2D(TypedDict):
1726+
x: int
1727+
y: int
1728+
label: NotRequired[str]
1729+
1730+
# Alternative syntax
1731+
Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': NotRequired[str]})
1732+
1733+
This means that a ``Point2D`` ``TypedDict`` can have the ``label``
1734+
key omitted.
1735+
1736+
It is also possible to mark all keys as non-required by default
1737+
by specifying a totality of ``False``.
17101738
Usage::
17111739

17121740
class Point2D(TypedDict, total=False):
@@ -1721,6 +1749,21 @@ These are not used in annotations. They are building blocks for declaring types.
17211749
``True`` as the value of the ``total`` argument. ``True`` is the default,
17221750
and makes all items defined in the class body required.
17231751

1752+
Individual keys of a ``total=False`` ``TypedDict`` can be marked as
1753+
required using ``Required``::
1754+
1755+
class Point2D(TypedDict, total=False):
1756+
x: Required[int]
1757+
y: Required[int]
1758+
label: str
1759+
1760+
# Alternative syntax
1761+
Point2D = TypedDict('Point2D', {
1762+
'x': Required[int],
1763+
'y': Required[int],
1764+
'label': str
1765+
}, total=False)
1766+
17241767
It is possible for a ``TypedDict`` type to inherit from one or more other ``TypedDict`` types
17251768
using the class-based syntax.
17261769
Usage::
@@ -1785,8 +1828,12 @@ These are not used in annotations. They are building blocks for declaring types.
17851828

17861829
``Point2D.__required_keys__`` and ``Point2D.__optional_keys__`` return
17871830
:class:`frozenset` objects containing required and non-required keys, respectively.
1788-
Currently the only way to declare both required and non-required keys in the
1789-
same ``TypedDict`` is mixed inheritance, declaring a ``TypedDict`` with one value
1831+
1832+
Keys marked with ``Required`` will always appear in ``__required_keys__``
1833+
and keys marked with ``NotRequired`` will always appear in ``__optional_keys__``.
1834+
1835+
It is also possible to use mixed inheritance to declare both required and
1836+
non-required keys in the same ``TypedDict`` , declaring a ``TypedDict`` with one value
17901837
for the ``total`` argument and then inheriting it from another ``TypedDict`` with
17911838
a different value for ``total``.
17921839
Usage::
@@ -1807,6 +1854,10 @@ These are not used in annotations. They are building blocks for declaring types.
18071854

18081855
.. versionadded:: 3.8
18091856

1857+
.. versionchanged:: 3.11
1858+
Added support for marking individual keys as ``Required`` or ``NotRequired``.
1859+
See :pep:`655`.
1860+
18101861
.. versionchanged:: 3.11
18111862
Added support for generic ``TypedDict``\ s.
18121863

0 commit comments

Comments
 (0)