PEP 655: Motivation: Enables mixed key requiredness in the TypedDict alternative syntax (#2359)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: CAM Gerlach <CAM.Gerlach@Gerlach.CAM>
This commit is contained in:
parent
7d3a7b9ebe
commit
71ace0e99d
18
pep-0655.rst
18
pep-0655.rst
|
@ -55,12 +55,26 @@ a mix of both required and potentially-missing keys:
|
||||||
title: str
|
title: str
|
||||||
year: NotRequired[int]
|
year: NotRequired[int]
|
||||||
|
|
||||||
|
This PEP also makes it possible to define TypedDicts in the
|
||||||
|
:pep:`alternative functional syntax <589#alternative-syntax>`
|
||||||
|
with a mix of required and potentially-missing keys,
|
||||||
|
which is not currently possible at all because the alternative syntax does
|
||||||
|
not support inheritance:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
Actor = TypedDict('Actor', {
|
||||||
|
'name': str,
|
||||||
|
# "in" is a keyword, so the functional syntax is necessary
|
||||||
|
'in': NotRequired[List[str]],
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
Rationale
|
Rationale
|
||||||
=========
|
=========
|
||||||
|
|
||||||
One might think it unusual to propose syntax that prioritizes marking
|
One might think it unusual to propose syntax that prioritizes marking
|
||||||
*required* keys rather than syntax for *potentially-missing* keys, as is
|
*required* keys rather than *potentially-missing* keys, as is
|
||||||
customary in other languages like TypeScript:
|
customary in other languages like TypeScript:
|
||||||
|
|
||||||
.. code-block:: typescript
|
.. code-block:: typescript
|
||||||
|
@ -139,7 +153,7 @@ same time:
|
||||||
year: NotRequired[Required[int]] # ERROR
|
year: NotRequired[Required[int]] # ERROR
|
||||||
|
|
||||||
|
|
||||||
The :pep:`alternative syntax <589#alternative-syntax>`
|
The :pep:`alternative functional syntax <589#alternative-syntax>`
|
||||||
for TypedDict also supports
|
for TypedDict also supports
|
||||||
``Required[]`` and ``NotRequired[]``:
|
``Required[]`` and ``NotRequired[]``:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue