Skip to content

Comments

Support for typing.NotRequired#17

Merged
giograno merged 3 commits intomainfrom
td-non-required-key
Feb 10, 2026
Merged

Support for typing.NotRequired#17
giograno merged 3 commits intomainfrom
td-non-required-key

Conversation

@giograno
Copy link
Member

In #11 we shipped some support for non-total TypedDict. In our implementation, we extend the original type of every attribute with str. With this trick, the serialization layer can put a special marker for keys that are not present in the dictionary.

TypedDict also has the possibility to use typing.NotRequired to achieve something similar.
For instance:

class EngineParameter(TypedDict):
    type_: str
    given_value: NotRequired[str | None]

In this case, the given_value key can be omitted, and the typechecker won't raise any concern.
Transforming the class as follows won't be correct, since now type_ could be omitted, while it is not in the case in the original implementation.

class EngineParameter(TypedDict, total=False):
    type_: str
    given_value: str | None

This PR implements the support for typing.NotRequired with the same logic as #11.

Closes PNX-601

@giograno giograno requested a review from bentsku February 10, 2026 08:45
@giograno giograno self-assigned this Feb 10, 2026
Copy link

@bentsku bentsku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice addition and nice find!

Here's the docs about the Required and NotRequired types: https://typing.python.org/en/latest/spec/typeddict.html#required-notrequired
Edit: and the PEP: https://peps.python.org/pep-0655/

Might be worth noting that total=False can also have Required, but let's not get stricter 😅

Only have a few comments nit, and a thought about the testing 👍 nice fix!

@giograno giograno merged commit b97265f into main Feb 10, 2026
6 checks passed
@giograno giograno deleted the td-non-required-key branch February 10, 2026 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants