@@ -45,6 +45,16 @@ different value for ``total``:
4545Having to declare two different TypedDict types for this purpose is
4646cumbersome.
4747
48+ This PEP introduces two new type qualifiers, ``typing.Required `` and
49+ ``typing.NotRequired ``, which allow defining a *single * TypedDict with
50+ a mix of both required and potentially-missing keys:
51+
52+ ::
53+
54+ class Movie(TypedDict):
55+ title: str
56+ year: NotRequired[int]
57+
4858
4959Rationale
5060=========
@@ -138,6 +148,35 @@ for TypedDict also supports
138148 Movie = TypedDict('Movie', {'name': str, 'year': NotRequired[int]})
139149
140150
151+ Interaction with ``total=False ``
152+ --------------------------------
153+
154+ Any :pep: `589 `-style TypedDict declared with ``total=False `` is equivalent
155+ to a TypedDict with an implicit ``total=True `` definition with all of its
156+ keys marked as ``NotRequired[] ``.
157+
158+ Therefore:
159+
160+ ::
161+
162+ class _MovieBase(TypedDict): # implicitly total=True
163+ title: str
164+
165+ class Movie(_MovieBase, total=False):
166+ year: int
167+
168+
169+ is equivalent to:
170+
171+ ::
172+
173+ class _MovieBase(TypedDict):
174+ title: str
175+
176+ class Movie(_MovieBase):
177+ year: NotRequired[int]
178+
179+
141180Interaction with ``Annotated[] ``
142181-----------------------------------
143182
@@ -162,8 +201,12 @@ annotations, which may always want ``Annotated[]`` as the outermost annotation.
162201[3 ]_
163202
164203
204+ Runtime behavior
205+ ----------------
206+
207+
165208Interaction with ``get_type_hints() ``
166- -------------------------------------
209+ '''''''''''''''''''''''''''''''''''''
167210
168211``typing.get_type_hints(...) `` applied to a TypedDict will by default
169212strip out any ``Required[] `` or ``NotRequired[] `` type qualifiers,
@@ -188,7 +231,7 @@ wishes to preserve *all* annotations in the original source:
188231
189232
190233Interaction with ``get_origin() `` and ``get_args() ``
191- ----------------------------------------------------
234+ ''''''''''''''''''''''''''''''''''''''''''''''''''''
192235
193236``typing.get_origin() `` and ``typing.get_args() `` will be updated to
194237recognize ``Required[] `` and ``NotRequired[] ``:
@@ -203,7 +246,7 @@ recognize ``Required[]`` and ``NotRequired[]``:
203246
204247
205248Interaction with ``__required_keys__ `` and ``__optional_keys__ ``
206- ----------------------------------------------------------------
249+ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
207250
208251An item marked with ``Required[] `` will always appear
209252in the ``__required_keys__ `` for its enclosing TypedDict. Similarly an item
@@ -226,6 +269,7 @@ How to Teach This
226269
227270To define a TypedDict where most keys are required and some are
228271potentially-missing, define a single TypedDict as normal
272+ (without the ``total `` keyword)
229273and mark those few keys that are potentially-missing with ``NotRequired[] ``.
230274
231275To define a TypedDict where most keys are potentially-missing and a few are
0 commit comments