@@ -82,6 +82,74 @@ T3 = TypeVar("T3", int, str, default=bytes) # E: TypeVar default must be one of
8282T4 = TypeVar("T4", int, str, default=Union[int, str]) # E: TypeVar default must be one of the constraint types
8383T5 = TypeVar("T5", float, str, default=int) # E: TypeVar default must be one of the constraint types
8484
85+ [case testTypeVarDefaultsInvalid3]
86+ from typing import Dict, Generic, TypeVar
87+
88+ T1 = TypeVar("T1")
89+ T2 = TypeVar("T2", default=T3) # E: Name "T3" is used before definition
90+ T3 = TypeVar("T3", default=str)
91+ T4 = TypeVar("T4", default=T3)
92+
93+ class ClassError1(Generic[T3, T1]): ... # E: "T1" cannot appear after "T3" in type parameter list because it has no default type
94+
95+ def func_error1(
96+ a: ClassError1,
97+ b: ClassError1[int],
98+ c: ClassError1[int, float],
99+ ) -> None:
100+ reveal_type(a) # N: Revealed type is "__main__.ClassError1[builtins.str, Any]"
101+ reveal_type(b) # N: Revealed type is "__main__.ClassError1[builtins.int, Any]"
102+ reveal_type(c) # N: Revealed type is "__main__.ClassError1[builtins.int, builtins.float]"
103+
104+ k = ClassError1()
105+ reveal_type(k) # N: Revealed type is "__main__.ClassError1[builtins.str, Any]"
106+ l = ClassError1[int]()
107+ reveal_type(l) # N: Revealed type is "__main__.ClassError1[builtins.int, Any]"
108+ m = ClassError1[int, float]()
109+ reveal_type(m) # N: Revealed type is "__main__.ClassError1[builtins.int, builtins.float]"
110+
111+ class ClassError2(Generic[T4, T3]): ... # E: Type parameter "T4" has a default type that refers to one or more type variables that are out of scope
112+
113+ def func_error2(
114+ a: ClassError2,
115+ b: ClassError2[int],
116+ c: ClassError2[int, float],
117+ ) -> None:
118+ reveal_type(a) # N: Revealed type is "__main__.ClassError2[Any, builtins.str]"
119+ reveal_type(b) # N: Revealed type is "__main__.ClassError2[builtins.int, builtins.str]"
120+ reveal_type(c) # N: Revealed type is "__main__.ClassError2[builtins.int, builtins.float]"
121+
122+ k = ClassError2()
123+ reveal_type(k) # N: Revealed type is "__main__.ClassError2[Any, builtins.str]"
124+ l = ClassError2[int]()
125+ reveal_type(l) # N: Revealed type is "__main__.ClassError2[builtins.int, builtins.str]"
126+ m = ClassError2[int, float]()
127+ reveal_type(m) # N: Revealed type is "__main__.ClassError2[builtins.int, builtins.float]"
128+
129+ TERR1 = Dict[T3, T1] # E: "T1" cannot appear after "T3" in type parameter list because it has no default type
130+
131+ def func_error_alias1(
132+ a: TERR1,
133+ b: TERR1[int],
134+ c: TERR1[int, float],
135+ ) -> None:
136+ reveal_type(a) # N: Revealed type is "builtins.dict[builtins.str, Any]"
137+ reveal_type(b) # N: Revealed type is "builtins.dict[builtins.int, Any]"
138+ reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
139+
140+ TERR2 = Dict[T4, T3] # TODO should be an error \
141+ # Type parameter "T4" has a default type that refers to one or more type variables that are out of scope
142+
143+ def func_error_alias2(
144+ a: TERR2,
145+ b: TERR2[int],
146+ c: TERR2[int, float],
147+ ) -> None:
148+ reveal_type(a) # N: Revealed type is "builtins.dict[Any, builtins.str]"
149+ reveal_type(b) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"
150+ reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
151+ [builtins fixtures/dict.pyi]
152+
85153[case testTypeVarDefaultsFunctions]
86154from typing import TypeVar, ParamSpec, List, Union, Callable, Tuple, overload
87155from typing_extensions import TypeVarTuple, Unpack
@@ -379,11 +447,12 @@ class ClassD1(Generic[T1, T2]): ...
379447
380448[case testTypeVarDefaultsClassRecursive1]
381449# flags: --disallow-any-generics
382- from typing import Generic, TypeVar
450+ from typing import Generic, TypeVar, List
383451
384452T1 = TypeVar("T1", default=str)
385453T2 = TypeVar("T2", default=T1)
386454T3 = TypeVar("T3", default=T2)
455+ T4 = TypeVar("T4", default=List[T1])
387456
388457class ClassD1(Generic[T1, T2]): ...
389458
@@ -425,12 +494,30 @@ def func_d2(
425494 n = ClassD2[int, float, str]()
426495 reveal_type(n) # N: Revealed type is "__main__.ClassD2[builtins.int, builtins.float, builtins.str]"
427496
497+ class ClassD3(Generic[T1, T4]): ...
498+
499+ def func_d3(
500+ a: ClassD3,
501+ b: ClassD3[int],
502+ c: ClassD3[int, float],
503+ ) -> None:
504+ reveal_type(a) # N: Revealed type is "__main__.ClassD3[builtins.str, builtins.list[builtins.str]]"
505+ reveal_type(b) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.list[builtins.int]]"
506+ reveal_type(c) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.float]"
507+
508+ # k = ClassD3()
509+ # reveal_type(k) # Revealed type is "__main__.ClassD3[builtins.str, builtins.list[builtins.str]]" # TODO
510+ l = ClassD3[int]()
511+ reveal_type(l) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.list[builtins.int]]"
512+ m = ClassD3[int, float]()
513+ reveal_type(m) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.float]"
514+
428515[case testTypeVarDefaultsClassRecursiveMultipleFiles]
429516# flags: --disallow-any-generics
430517from typing import Generic, TypeVar
431518from file2 import T as T2
432519
433- T = TypeVar('T' , default=T2)
520+ T = TypeVar("T" , default=T2)
434521
435522class ClassG1(Generic[T2, T]):
436523 pass
@@ -615,3 +702,22 @@ def func_c4(
615702 # reveal_type(b) # Revealed type is "Tuple[builtins.int, builtins.str]" # TODO
616703 reveal_type(c) # N: Revealed type is "Tuple[builtins.int, builtins.float]"
617704[builtins fixtures/tuple.pyi]
705+
706+ [case testTypeVarDefaultsTypeAliasRecursive1]
707+ # flags: --disallow-any-generics
708+ from typing import Dict, List, TypeVar
709+
710+ T1 = TypeVar("T1")
711+ T2 = TypeVar("T2", default=T1)
712+
713+ TD1 = Dict[T1, T2]
714+
715+ def func_d1(
716+ a: TD1, # E: Missing type parameters for generic type "TD1"
717+ b: TD1[int],
718+ c: TD1[int, float],
719+ ) -> None:
720+ reveal_type(a) # N: Revealed type is "builtins.dict[Any, Any]"
721+ reveal_type(b) # N: Revealed type is "builtins.dict[builtins.int, builtins.int]"
722+ reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
723+ [builtins fixtures/dict.pyi]
0 commit comments