@@ -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
+ 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
+
1025
1039
.. data :: Annotated
1026
1040
1027
1041
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.
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.
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 ``::
1711
1738
1712
1739
class Point2D(TypedDict, total=False):
1713
1740
x: int
@@ -1721,6 +1748,21 @@ These are not used in annotations. They are building blocks for declaring types.
1721
1748
``True `` as the value of the ``total `` argument. ``True `` is the default,
1722
1749
and makes all items defined in the class body required.
1723
1750
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
+
1724
1766
It is possible for a ``TypedDict `` type to inherit from one or more other ``TypedDict `` types
1725
1767
using the class-based syntax.
1726
1768
Usage::
@@ -1785,11 +1827,16 @@ These are not used in annotations. They are building blocks for declaring types.
1785
1827
1786
1828
``Point2D.__required_keys__ `` and ``Point2D.__optional_keys__ `` return
1787
1829
: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 ``::
1793
1840
1794
1841
>>> class Point2D(TypedDict, total=False):
1795
1842
... x: int
@@ -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 :data: `Required ` or :data: `NotRequired `.
1859
+ See :pep: `655 `.
1860
+
1810
1861
.. versionchanged :: 3.11
1811
1862
Added support for generic ``TypedDict ``\ s.
1812
1863
0 commit comments