Type Hint for typing.TypedDict
NotRequired
: New in Python 3.11.
Recipes
from typing import TypedDict, NotRequired, Unpack
class KwArgs(TypedDict):
key_a: int
key_b: float
label: NotRequired[str]
# functional syntax: alternative syntax
KwArgs = TypedDict('MyDict', {'key_a': int, 'key_b': float, 'label': NotRequired})
# Using `Unpack` since Python 3.12.
def func(**kwargs: Unpack[KwArgs]) -> None:
pass
d: KwArgs = {'key_a': 1, 'key_b': 1.2}
func(**d)
func(key_a=1, key_b=1.2)
d1: dict[int, float] = {'key_a': 1, 'key_b': 1.2}
func(**d1) # WRONG!