CKTIS / Python
typing.NotRequired
Mark an individual TypedDict key as omissible, even when the other keys are required by default.
Version: Python 3.14; repository image, production runtime not attested
Syntax
note: NotRequired[str]Mark an individual TypedDict key as omissible, even when the other keys are required by default.
Omitting a key differs from supplying None for a field that expects a string. Ordinary dictionary lookup still raises KeyError for an absent item.
The optional key can be discovered through the schema’s __optional_keys__ attribute.
Examples
from typing import NotRequired, TypedDict
class Job(TypedDict):
key: int
note: NotRequired[str]
job: Job = {"key": 2}
print(sorted(Job.__optional_keys__), "note" in job)
try:
print(job["note"])
except KeyError:
print("absent")
Result
['note'] False absent
The optional key can be discovered through the schema’s `__optional_keys__` attribute.