@@ -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+ Special typing constructs that mark individual keys of a :class: `TypedDict `
1032+ as either required or non-required respectively.
1033+
1034+ For more information, see :class: `TypedDict ` and
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,8 +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.
1710- Usage::
1723+ mark individual keys as non-required using :data: `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 ``::
17111738
17121739 class Point2D(TypedDict, total=False):
17131740 x: int
@@ -1721,6 +1748,21 @@ These are not used in annotations. They are building blocks for declaring types.
17211748 ``True `` as the value of the ``total `` argument. ``True `` is the default,
17221749 and makes all items defined in the class body required.
17231750
1751+ Individual keys of a ``total=False `` ``TypedDict `` can be marked as
1752+ required using :data: `Required `::
1753+
1754+ class Point2D(TypedDict, total=False):
1755+ x: Required[int]
1756+ y: Required[int]
1757+ label: str
1758+
1759+ # Alternative syntax
1760+ Point2D = TypedDict('Point2D', {
1761+ 'x': Required[int],
1762+ 'y': Required[int],
1763+ 'label': str
1764+ }, total=False)
1765+
17241766 It is possible for a ``TypedDict `` type to inherit from one or more other ``TypedDict `` types
17251767 using the class-based syntax.
17261768 Usage::
@@ -1785,11 +1827,16 @@ These are not used in annotations. They are building blocks for declaring types.
17851827
17861828 ``Point2D.__required_keys__ `` and ``Point2D.__optional_keys__ `` return
17871829 :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
1790- for the ``total `` argument and then inheriting it from another ``TypedDict `` with
1791- a different value for ``total ``.
1792- Usage::
1830+
1831+ Keys marked with :data: `Required ` will always appear in ``__required_keys__ ``
1832+ and keys marked with :data: `NotRequired ` will always appear in ``__optional_keys__ ``.
1833+
1834+ For backwards compatibility with Python 3.10 and below,
1835+ it is also possible to use inheritance to declare both required and
1836+ non-required keys in the same ``TypedDict `` . This is done by declaring a
1837+ ``TypedDict `` with one value for the ``total `` argument and then
1838+ inheriting from it in another ``TypedDict `` with a different value for
1839+ ``total ``::
17931840
17941841 >>> class Point2D(TypedDict, total=False):
17951842 ... x: int
@@ -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 :data: `Required ` or :data: `NotRequired `.
1859+ See :pep: `655 `.
1860+
18101861 .. versionchanged :: 3.11
18111862 Added support for generic ``TypedDict ``\ s.
18121863
0 commit comments