2Tests the TypeAliasType call introduced in Python 3.12.
5from typing import Callable, Generic, ParamSpec, TypeAliasType, TypeVar, TypeVarTuple
9TStr = TypeVar("TStr", bound=str)
11Ts = TypeVarTuple("Ts")
16GoodAlias1 = TypeAliasType("GoodAlias1", int)
17GoodAlias2 = TypeAliasType("GoodAlias2", list[T], type_params=(T,))
18GoodAlias3 = TypeAliasType("GoodAlias3", list[T] | list[S], type_params=(S, T))
19GoodAlias4 = TypeAliasType("GoodAlias4", T | "list[GoodAlias4[T]]", type_params=(T,))
20GoodAlias5 = TypeAliasType(
22 Callable[P, TStr] | list[S] | list["GoodAlias5[S, TStr, P]"] | tuple[*Ts],
23 type_params=(S, TStr, P, Ts),
Unexpected error
[invalid-argument-type] Argument to class `TypeAliasType` is incorrect: Expected `tuple[TypeVar | ParamSpec | typing_extensions.TypeVarTuple, ...]`, found `tuple[TypeVar, TypeVar, ParamSpec, typing.TypeVarTuple]`
26class ClassA(Generic[T]):
27 GoodAlias6 = TypeAliasType("GoodAlias6", list[T])
30print(GoodAlias1.__value__) # OK
31print(GoodAlias1.__type_params__) # OK
32print(GoodAlias1.other_attrib) # E: unknown attribute
[unresolved-attribute] Object of type `TypeAliasType` has no attribute `other_attrib`
35x1: GoodAlias4[int] = 1 # OK
36x2: GoodAlias4[int] = [1] # OK
37x3: GoodAlias5[str, str, ..., int, str] # OK
38x4: GoodAlias5[int, str, ..., int, str] # OK
39x5: GoodAlias5[int, str, [int, str], *tuple[int, str, int]] # OK
40x6: GoodAlias5[int, int, ...] # E: incorrect type arguments
Expected a ty diagnostic for this line
43BadAlias1 = TypeAliasType("BadAlias1", list[S], type_params=(T,)) # E: S not in scope
Expected a ty diagnostic for this line
44BadAlias2 = TypeAliasType("BadAlias2", list[S]) # E: S not in scope
Expected a ty diagnostic for this line
45BadAlias3 = TypeAliasType("BadAlias3", int, type_params=my_tuple) # E: not literal tuple
Expected a ty diagnostic for this line
46BadAlias4 = TypeAliasType("BadAlias4", "BadAlias4") # E: circular dependency
Expected a ty diagnostic for this line
47BadAlias5 = TypeAliasType("BadAlias5", T | "BadAlias5[str]", type_params=(T,)) # E: circular dependency
Expected a ty diagnostic for this line
48BadAlias6 = TypeAliasType("BadAlias6", "BadAlias7") # E: circular dependency
Expected a ty diagnostic for this line
49BadAlias7 = TypeAliasType("BadAlias7", BadAlias6) # E?: circular dependency
51# The following are invalid type expressions for a type alias.
52BadAlias8 = TypeAliasType("BadAlias8", eval("".join(map(chr, [105, 110, 116])))) # E
[invalid-type-form] Function calls are not allowed in type expressions
53BadAlias9 = TypeAliasType("BadAlias9", [int, str]) # E
[invalid-type-form] List literals are not allowed in this context in a type expression: Did you mean `tuple[int, str]`?
54BadAlias10 = TypeAliasType("BadAlias10", ((int, str),)) # E
[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]`?
55BadAlias11 = TypeAliasType("BadAlias11", [int for i in range(1)]) # E
[invalid-type-form] List comprehensions are not allowed in type expressions
56BadAlias12 = TypeAliasType("BadAlias12", {"a": "b"}) # E
[invalid-type-form] Dict literals are not allowed in type expressions
57BadAlias13 = TypeAliasType("BadAlias13", (lambda: int)()) # E
[invalid-type-form] Function calls are not allowed in type expressions
58BadAlias14 = TypeAliasType("BadAlias14", [int][0]) # E
[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
59BadAlias15 = TypeAliasType("BadAlias15", int if 1 < 3 else str) # E
[invalid-type-form] `if` expressions are not allowed in type expressions
60BadAlias16 = TypeAliasType("BadAlias16", var1) # E
[invalid-type-form] Variable of type `Literal[3]` is not allowed in a type expression
61BadAlias17 = TypeAliasType("BadAlias17", True) # E
[invalid-type-form] Boolean literals are not allowed in this context in a type expression
62BadAlias18 = TypeAliasType("BadAlias18", 1) # E
[invalid-type-form] Int literals are not allowed in this context in a type expression
63BadAlias19 = TypeAliasType("BadAlias19", list or set) # E
[invalid-type-form] Boolean operations are not allowed in type expressions
64BadAlias20 = TypeAliasType("BadAlias20", f"{'int'}") # E
[invalid-type-form] F-strings are not allowed in type expressions
66BadAlias21 = TypeAliasType("BadAlias21", list[BadAlias21]) # E
[unresolved-reference] Name `BadAlias21` used when not defined