Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 655: Motivation: Enables mixed key requiredness in the TypedDict alternative syntax #2359

Merged
merged 4 commits into from
Feb 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pep-0655.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

::
davidfstr marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down Expand Up @@ -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[]``:

Expand Down