2Tests the "type" statement introduced in Python 3.12.
5from typing import Callable, TypeVar
9type GoodAlias2[S1, *S2, **S3] = Callable[S3, S1] | tuple[*S2]
10type GoodAlias3 = GoodAlias2[int, tuple[int, str], ...]
Unexpected error
[invalid-type-arguments] Too many type arguments: expected 2, got 3
14 type GoodAlias4 = int | None
17GoodAlias1.bit_count # E: cannot access attribute
[unresolved-attribute] Object of type `TypeAliasType` has no attribute `bit_count`
19GoodAlias1() # E: cannot call alias
[call-non-callable] Object of type `TypeAliasType` is not callable
21print(GoodAlias1.__value__) # OK
22print(GoodAlias1.__type_params__) # OK
23print(GoodAlias1.other_attrib) # E: unknown attribute
[unresolved-attribute] Object of type `TypeAliasType` has no attribute `other_attrib`
26class DerivedInt(GoodAlias1): # E: cannot use alias as base class
[invalid-base] Invalid class base with type `TypeAliasType`
31 if isinstance(x, GoodAlias1): # E: cannot use alias in isinstance
[invalid-argument-type] Argument to function `isinstance` is incorrect: Expected `type | UnionType | tuple[Divergent, ...]`, found `TypeAliasType`
36# The following should not be allowed as type aliases.
37type BadTypeAlias1 = eval("".join(map(chr, [105, 110, 116]))) # E
[invalid-type-form] Function calls are not allowed in type expressions
38type BadTypeAlias2 = [int, str] # E
[invalid-type-form] List literals are not allowed in this context in a type expression: Did you mean `tuple[int, str]`?
39type BadTypeAlias3 = ((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]`?
40type BadTypeAlias4 = [int for i in range(1)] # E
[invalid-type-form] List comprehensions are not allowed in type expressions
41type BadTypeAlias5 = {"a": "b"} # E
[invalid-type-form] Dict literals are not allowed in type expressions
42type BadTypeAlias6 = (lambda: int)() # E
[invalid-type-form] Function calls are not allowed in type expressions
43type BadTypeAlias7 = [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
44type BadTypeAlias8 = int if 1 < 3 else str # E
[invalid-type-form] `if` expressions are not allowed in type expressions
45type BadTypeAlias9 = var1 # E
[invalid-type-form] Variable of type `Literal[1]` is not allowed in a type expression
46type BadTypeAlias10 = True # E
[invalid-type-form] Boolean literals are not allowed in this context in a type expression
47type BadTypeAlias11 = 1 # E
[invalid-type-form] Int literals are not allowed in this context in a type expression
48type BadTypeAlias12 = list or set # E
[invalid-type-form] Boolean operations are not allowed in type expressions
49type BadTypeAlias13 = f"{'int'}" # E
[fstring-type-annotation] Type expressions cannot use f-strings
51type BadTypeAlias14 = int # E[TA14]: redeclared
Expected a ty diagnostic for this line (tag 'TA14')
52type BadTypeAlias14 = int # E[TA14]: redeclared
Expected a ty diagnostic for this line (tag 'TA14')
56 type BadTypeAlias15 = int # E: alias not allowed in function
Expected a ty diagnostic for this line
62type TA1[K] = dict[K, V] # E: combines old and new TypeVars
Expected a ty diagnostic for this line
67type TA2 = list[T1] # E: uses old TypeVar
Expected a ty diagnostic for this line
70type RecursiveTypeAlias1[T] = T | list[RecursiveTypeAlias1[T]]
72r1_1: RecursiveTypeAlias1[int] = 1
73r1_2: RecursiveTypeAlias1[int] = [1, [1, 2, 3]]
75type RecursiveTypeAlias2[S: int, T: str, **P] = Callable[P, T] | list[S] | list[RecursiveTypeAlias2[S, T, P]]
77r2_1: RecursiveTypeAlias2[str, str, ...] # E: not compatible with S bound
[invalid-type-arguments] Type `str` is not assignable to upper bound `int` of type variable `S@RecursiveTypeAlias2`
78r2_2: RecursiveTypeAlias2[int, str, ...]
79r2_3: RecursiveTypeAlias2[int, int, ...] # E: not compatible with T bound
[invalid-type-arguments] Type `int` is not assignable to upper bound `str` of type variable `T@RecursiveTypeAlias2`
80r2_4: RecursiveTypeAlias2[int, str, [int, str]]
82type RecursiveTypeAlias3 = RecursiveTypeAlias3 # E: circular definition
[cyclic-type-alias-definition] Cyclic definition of `RecursiveTypeAlias3`
84type RecursiveTypeAlias4[T] = T | RecursiveTypeAlias4[str] # E: circular definition
Expected a ty diagnostic for this line
86type RecursiveTypeAlias5[T] = T | list[RecursiveTypeAlias5[T]]
88type RecursiveTypeAlias6 = RecursiveTypeAlias7 # E[RTA6+]: circular definition
Tag 'RTA6'
[cyclic-type-alias-definition] Cyclic definition of `RecursiveTypeAlias6`
89type RecursiveTypeAlias7 = RecursiveTypeAlias6 # E[RTA6+]: circular definition
Tag 'RTA6'
[cyclic-type-alias-definition] Cyclic definition of `RecursiveTypeAlias7`