@@ -1085,17 +1085,18 @@ T1 = T2 = TypeAliasType("T", int)
1085
1085
t1: T1 # E: Variable "__main__.T1" is not valid as a type \
1086
1086
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
1087
1087
1088
- T3 = TypeAliasType("T3", -1)
1089
- t3: T3 # E: Variable "__main__.T3" is not valid as a type \
1090
- # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs- type-aliases
1088
+ T3 = TypeAliasType("T3", -1) # E: Invalid type: try using Literal[-1] instead?
1089
+ t3: T3
1090
+ reveal_type(t3) # N: Revealed type is "Any"
1091
1091
[builtins fixtures/tuple.pyi]
1092
1092
1093
1093
[case testTypeAliasTypeGeneric]
1094
- from typing_extensions import TypeAliasType, TypeVarTuple, ParamSpec
1095
- from typing import Callable, Dict, TypeVar, Tuple , Unpack
1094
+ from typing import Callable, Dict, Generic, TypeVar, Tuple
1095
+ from typing_extensions import TypeAliasType, TypeVarTuple, ParamSpec , Unpack
1096
1096
1097
1097
K = TypeVar('K')
1098
1098
V = TypeVar('V')
1099
+ T = TypeVar('T')
1099
1100
Ts = TypeVarTuple("Ts")
1100
1101
Ts1 = TypeVarTuple("Ts1")
1101
1102
P = ParamSpec("P")
@@ -1123,14 +1124,20 @@ def g(x: int, y: float) -> int: return 1
1123
1124
xp1: ParamAlias[str, float] = f
1124
1125
xp2: ParamAlias[str, float] = g # E: Incompatible types in assignment (expression has type "Callable[[int, float], int]", variable has type "Callable[[str, float], int]")
1125
1126
xp3: ParamAlias[str, float] = lambda x, y: 1
1127
+
1128
+ class G(Generic[P, T]): ...
1129
+ ParamAlias2 = TypeAliasType("ParamAlias2", G[P, T], type_params=(P, T))
1130
+ xp: ParamAlias2[[int], str]
1131
+ reveal_type(xp) # N: Revealed type is "__main__.G[[builtins.int], builtins.str]"
1126
1132
[builtins fixtures/dict.pyi]
1127
1133
1128
1134
[case testTypeAliasTypeInvalidGeneric]
1129
1135
from typing_extensions import TypeAliasType, TypeVarTuple, ParamSpec
1130
- from typing import Dict, TypeVar, Tuple, Unpack
1136
+ from typing import Callable, Dict, Generic , TypeVar, Tuple, Unpack
1131
1137
1132
1138
K = TypeVar('K')
1133
1139
V = TypeVar('V')
1140
+ T = TypeVar('T')
1134
1141
Ts = TypeVarTuple("Ts")
1135
1142
Ts1 = TypeVarTuple("Ts1")
1136
1143
P = ParamSpec("P")
@@ -1139,7 +1146,7 @@ Ta1 = TypeAliasType("Ta1", int, type_params=K) # E: Tuple literal expected as t
1139
1146
1140
1147
Ta2 = TypeAliasType("Ta2", int, type_params=(None,)) # E: Free type variable expected in type_params argument to TypeAliasType
1141
1148
1142
- Ta3 = TypeAliasType("Ta3", Dict[K, V], type_params=(V,)) # E: Can't use bound type variable "K" to define generic alias
1149
+ Ta3 = TypeAliasType("Ta3", Dict[K, V], type_params=(V,)) # E: Type variable "K" is not included in type_params
1143
1150
partially_generic1: Ta3[int] = {"a": 1}
1144
1151
reveal_type(partially_generic1) # N: Revealed type is "builtins.dict[Any, builtins.int]"
1145
1152
partially_generic2: Ta3[int] = {1: "a"} # E: Dict entry 0 has incompatible type "int": "str"; expected "Any": "int"
@@ -1148,6 +1155,25 @@ Ta4 = TypeAliasType("Ta4", Tuple[Unpack[Ts]], type_params=(Ts, Ts1)) # E: Can o
1148
1155
1149
1156
Ta5 = TypeAliasType("Ta5", Dict) # Unlike old style aliases, this is not generic
1150
1157
non_generic_dict: Ta5[int, str] # E: Bad number of arguments for type alias, expected 0, given 2
1158
+ reveal_type(non_generic_dict) # N: Revealed type is "builtins.dict[Any, Any]"
1159
+
1160
+ Ta6 = TypeAliasType("Ta6", Tuple[Unpack[Ts]]) # E: TypeVarTuple "Ts" is not included in type_params
1161
+ unbound_tvt_alias: Ta6[int] # E: Bad number of arguments for type alias, expected 0, given 1
1162
+ reveal_type(unbound_tvt_alias) # N: Revealed type is "builtins.tuple[Any, ...]"
1163
+
1164
+ class G(Generic[P, T]): ...
1165
+ Ta7 = TypeAliasType("Ta7", G[P, T]) # E: ParamSpec "P" is not included in type_params \
1166
+ # E: Type variable "T" is not included in type_params
1167
+ unbound_ps_alias: Ta7[[int], str] # E: Bracketed expression "[...]" is not valid as a type \
1168
+ # N: Did you mean "List[...]"? \
1169
+ # E: Bad number of arguments for type alias, expected 0, given 2
1170
+ reveal_type(unbound_ps_alias) # N: Revealed type is "__main__.G[Any, Any]"
1171
+
1172
+ # TODO this does not work yet, it should report unbound P
1173
+ # Ta8 = TypeAliasType("Ta8", Callable[P, int])
1174
+ # unbound_ps_alias: Ta8[int]
1175
+ # reveal_type(unbound_ps_alias)
1176
+
1151
1177
1152
1178
[builtins fixtures/dict.pyi]
1153
1179
0 commit comments