Skip to content

Commit 71ace0e

Browse files
davidfstrAlexWaygoodCAM-Gerlach
authored
PEP 655: Motivation: Enables mixed key requiredness in the TypedDict alternative syntax (#2359)
Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: CAM Gerlach <[email protected]>
1 parent 7d3a7b9 commit 71ace0e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pep-0655.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,26 @@ a mix of both required and potentially-missing keys:
5555
title: str
5656
year: NotRequired[int]
5757

58+
This PEP also makes it possible to define TypedDicts in the
59+
:pep:`alternative functional syntax <589#alternative-syntax>`
60+
with a mix of required and potentially-missing keys,
61+
which is not currently possible at all because the alternative syntax does
62+
not support inheritance:
63+
64+
::
65+
66+
Actor = TypedDict('Actor', {
67+
'name': str,
68+
# "in" is a keyword, so the functional syntax is necessary
69+
'in': NotRequired[List[str]],
70+
})
71+
5872

5973
Rationale
6074
=========
6175

6276
One might think it unusual to propose syntax that prioritizes marking
63-
*required* keys rather than syntax for *potentially-missing* keys, as is
77+
*required* keys rather than *potentially-missing* keys, as is
6478
customary in other languages like TypeScript:
6579

6680
.. code-block:: typescript
@@ -139,7 +153,7 @@ same time:
139153
year: NotRequired[Required[int]] # ERROR
140154

141155

142-
The :pep:`alternative syntax <589#alternative-syntax>`
156+
The :pep:`alternative functional syntax <589#alternative-syntax>`
143157
for TypedDict also supports
144158
``Required[]`` and ``NotRequired[]``:
145159

0 commit comments

Comments
 (0)