2Tests the Required and NotRequired special forms.
5# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#required-and-notrequired
7from typing import Annotated, NotRequired, Required, TypedDict
10# Required and NotRequired are valid only within a TypedDict.
12 x: Required[int] # E: Required not allowed in this context
Expected a ty diagnostic for this line
16 x: NotRequired[int], # E: NotRequired not allowed in this context
[invalid-type-form] `NotRequired` is not allowed in function parameter annotations
21class TD1(TypedDict, total=False):
25class TD2(TD1, total=True):
34class TD4(TypedDict, total=False):
39class TD5(TypedDict, total=True):
48# These are all equivalent types, so they should be
49# bidirectionally type compatible.
59 a: Required[Required[int]] # E: Nesting not allowed
Expected a ty diagnostic for this line
60 b: Required[NotRequired[int]] # E: Nesting not allowed
Expected a ty diagnostic for this line
64 # > Required[] and NotRequired[] can be used with Annotated[], in any nesting order.
65 x: Annotated[Required[int], ""]
66 y: Required[Annotated[int, ""]]
67 z: Annotated[Required[Annotated[int, ""]], ""]
70RecursiveMovie = TypedDict(
71 "RecursiveMovie", {"title": Required[str], "predecessor": NotRequired["RecursiveMovie"]}
74movie: RecursiveMovie = {"title": "Beethoven 3", "predecessor": {"title": "Beethoven 2"}}