2Tests the typing.Annotated special form.
5# Specification: https://typing.readthedocs.io/en/latest/spec/qualifiers.html#annotated
7# > The first argument to Annotated must be a valid type
9from dataclasses import InitVar, dataclass
28Good1: Annotated[Union[int, str], ""]
29Good2: Annotated[int | None, ""]
30Good3: Annotated[list[int], ""]
31Good4: Annotated[Any, ""]
32Good5: Annotated[tuple[int, ...] | list[int], ""]
33Good6: Annotated[Callable[..., int], ""]
34Good7: Annotated["int | str", ""]
35Good8: Annotated[list["int | str"], ""]
38Bad1: Annotated[[int, str], ""] # E: invalid type expression
[invalid-type-form] List literals are not allowed in this context in a type expression: Did you mean `tuple[int, str]`?
39Bad2: Annotated[((int, str),), ""] # E: invalid type expression
[invalid-type-form] Tuple literals are not allowed in this context in a type expression
[invalid-type-form] Tuple literals are not allowed in this context in a type expression: Did you mean `tuple[int, str]`?
40Bad3: Annotated[[int for i in range(1)], ""] # E: invalid type expression
[invalid-type-form] List comprehensions are not allowed in type expressions
41Bad4: Annotated[{"a": "b"}, ""] # E: invalid type expression
[invalid-type-form] Dict literals are not allowed in type expressions
42Bad5: Annotated[(lambda: int)(), ""] # E: invalid type expression
[invalid-type-form] Function calls are not allowed in type expressions
43Bad6: Annotated[[int][0], ""] # E: invalid type expression
[invalid-type-form] Invalid subscript of object of type `list[Unknown | <class 'int'>]` in type expression
[invalid-type-form] Int literals are not allowed in this context in a type expression
44Bad7: Annotated[int if 1 < 3 else str, ""] # E: invalid type expression
[invalid-type-form] `if` expressions are not allowed in type expressions
45Bad8: Annotated[var1, ""] # E: invalid type expression
[unresolved-reference] Name `var1` used when not defined
46Bad9: Annotated[True, ""] # E: invalid type expression
[invalid-type-form] Boolean literals are not allowed in this context in a type expression
47Bad10: Annotated[1, ""] # E: invalid type expression
[invalid-type-form] Int literals are not allowed in this context in a type expression
48Bad11: Annotated[list or set, ""] # E: invalid type expression
[invalid-type-form] Boolean operations are not allowed in type expressions
49Bad12: Annotated[f"{'int'}", ""] # E: invalid type expression
[fstring-type-annotation] Type expressions cannot use f-strings
52# > Multiple type annotations are supported (Annotated supports variadic arguments):
54Multi1: Annotated[int | str, 3, ""]
55Multi2: Annotated[int | str, 3, "", lambda x: x, max(1, 2)]
57# > Annotated must be called with at least two arguments ( Annotated[int] is not valid)
59Bad13: Annotated[int] # E: requires at least two arguments
[invalid-type-form] Special form `typing.Annotated` expected at least 2 arguments (one type and at least one metadata element)
62# > Annotated types can be nested
64Nested1: list[Annotated[dict[str, Annotated[str, ""]], ""]]
65Nested2: Annotated[list[Annotated[dict[str, Annotated[Literal[1, 2, 3], ""]], ""]], ""]
68# > Annotated is not type compatible with type or type[T]
69SmallInt: TypeAlias = Annotated[int, ""]
71not_type1: type[Any] = Annotated[int, ""] # E
[invalid-assignment] Object of type `<special-form 'typing.Annotated[int, <metadata>]'>` is not assignable to `type[Any]`
72not_type2: type[Any] = SmallInt # E
[invalid-assignment] Object of type `<special-form 'typing.Annotated[int, <metadata>]'>` is not assignable to `type[Any]`
75def func4(x: type[T]) -> T:
79func4(Annotated[str, ""]) # E
[invalid-argument-type] Argument to function `func4` is incorrect: Expected `type[Unknown]`, found `<special-form 'typing.Annotated[str, <metadata>]'>`
[invalid-argument-type] Argument to function `func4` is incorrect: Expected `type[Unknown]`, found `<special-form 'typing.Annotated[int, <metadata>]'>`
83# > An attempt to call Annotated (whether parameterized or not) should be
84# > treated as a type error by type checkers.
[call-non-callable] Object of type `<special-form 'typing.Annotated'>` is not callable
87Annotated[int, ""]() # E
[call-non-callable] Object of type `GenericAlias` is not callable
[call-non-callable] Object of type `GenericAlias` is not callable
92 a: ClassVar[Annotated[int, ""]] = 1
93 b: Annotated[ClassVar[int], ""] = 1
94 c: Final[Annotated[int, ""]] = 1
95 d: Annotated[Final[int], ""] = 1
100 a: InitVar[Annotated[int, ""]]
101 b: Annotated[InitVar[int], ""]
104class ClassC(TypedDict):
105 a: Annotated[Required[int], ""]
106 b: Required[Annotated[int, ""]]
107 c: Annotated[NotRequired[int], ""]
108 d: NotRequired[Annotated[int, ""]]
111TA1: TypeAlias = Annotated[int | str, ""]
112TA2 = Annotated[Literal[1, 2], ""]
113TA3 = Annotated[T, ""]