@@ -78,6 +78,8 @@ annotations. These include:
78
78
*Introducing * :data: `TypeVarTuple `
79
79
* :pep: `647 `: User-Defined Type Guards
80
80
*Introducing * :data: `TypeGuard `
81
+ * :pep: `655 `: Marking individual TypedDict items as required or potentially-missing
82
+ *Introducing * :data: `Required ` and :data: `NotRequired `
81
83
* :pep: `673 `: Self type
82
84
*Introducing * :data: `Self `
83
85
* :pep: `675 `: Arbitrary Literal String Type
@@ -1022,6 +1024,18 @@ These can be used as types in annotations using ``[]``, each having a unique syn
1022
1024
1023
1025
.. versionadded :: 3.8
1024
1026
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
+
1025
1039
.. data :: Annotated
1026
1040
1027
1041
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.
1706
1720
Point2D = TypedDict('Point2D', {'in': int, 'x-y': int})
1707
1721
1708
1722
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 ``.
1710
1738
Usage::
1711
1739
1712
1740
class Point2D(TypedDict, total=False):
@@ -1721,6 +1749,21 @@ These are not used in annotations. They are building blocks for declaring types.
1721
1749
``True `` as the value of the ``total `` argument. ``True `` is the default,
1722
1750
and makes all items defined in the class body required.
1723
1751
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
+
1724
1767
It is possible for a ``TypedDict `` type to inherit from one or more other ``TypedDict `` types
1725
1768
using the class-based syntax.
1726
1769
Usage::
@@ -1785,8 +1828,12 @@ These are not used in annotations. They are building blocks for declaring types.
1785
1828
1786
1829
``Point2D.__required_keys__ `` and ``Point2D.__optional_keys__ `` return
1787
1830
: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
1790
1837
for the ``total `` argument and then inheriting it from another ``TypedDict `` with
1791
1838
a different value for ``total ``.
1792
1839
Usage::
@@ -1807,6 +1854,10 @@ These are not used in annotations. They are building blocks for declaring types.
1807
1854
1808
1855
.. versionadded :: 3.8
1809
1856
1857
+ .. versionchanged :: 3.11
1858
+ Added support for marking individual keys as ``Required `` or ``NotRequired ``.
1859
+ See :pep: `655 `.
1860
+
1810
1861
.. versionchanged :: 3.11
1811
1862
Added support for generic ``TypedDict ``\ s.
1812
1863
0 commit comments