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:
David Foster 2022-02-25 10:21:02 -05:00 committed by GitHub
parent 7d3a7b9ebe
commit 71ace0e99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 2 deletions

View File

@ -55,12 +55,26 @@ a mix of both required and potentially-missing keys:
title: str
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
=========
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:
.. code-block:: typescript
@ -139,7 +153,7 @@ same time:
year: NotRequired[Required[int]] # ERROR
The :pep:`alternative syntax <589#alternative-syntax>`
The :pep:`alternative functional syntax <589#alternative-syntax>`
for TypedDict also supports
``Required[]`` and ``NotRequired[]``: