@@ -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
87155from typing_extensions import TypeVarTuple, Unpack
@@ -351,11 +419,12 @@ def func_c4(
351419
352420[case testTypeVarDefaultsClassRecursive1]
353421# flags: --disallow-any-generics
354- from typing import Generic, TypeVar
422+ from typing import Generic, TypeVar, List
355423
356424T1 = TypeVar("T1", default=str)
357425T2 = TypeVar("T2", default=T1)
358426T3 = TypeVar("T3", default=T2)
427+ T4 = TypeVar("T4", default=List[T1])
359428
360429class ClassD1(Generic[T1, T2]): ...
361430
@@ -397,12 +466,30 @@ def func_d2(
397466 n = ClassD2[int, float, str]()
398467 reveal_type(n) # N: Revealed type is "__main__.ClassD2[builtins.int, builtins.float, builtins.str]"
399468
469+ class ClassD3(Generic[T1, T4]): ...
470+
471+ def func_d3(
472+ a: ClassD3,
473+ b: ClassD3[int],
474+ c: ClassD3[int, float],
475+ ) -> None:
476+ reveal_type(a) # N: Revealed type is "__main__.ClassD3[builtins.str, builtins.list[builtins.str]]"
477+ reveal_type(b) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.list[builtins.int]]"
478+ reveal_type(c) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.float]"
479+
480+ # k = ClassD3()
481+ # reveal_type(k) # Revealed type is "__main__.ClassD3[builtins.str, builtins.list[builtins.str]]" # TODO
482+ l = ClassD3[int]()
483+ reveal_type(l) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.list[builtins.int]]"
484+ m = ClassD3[int, float]()
485+ reveal_type(m) # N: Revealed type is "__main__.ClassD3[builtins.int, builtins.float]"
486+
400487[case testTypeVarDefaultsClassRecursiveMultipleFiles]
401488# flags: --disallow-any-generics
402489from typing import Generic, TypeVar
403490from file2 import T as T2
404491
405- T = TypeVar('T' , default=T2)
492+ T = TypeVar("T" , default=T2)
406493
407494class ClassG1(Generic[T2, T]):
408495 pass
@@ -587,3 +674,46 @@ def func_c4(
587674 # reveal_type(b) # Revealed type is "Tuple[builtins.int, builtins.str]" # TODO
588675 reveal_type(c) # N: Revealed type is "Tuple[builtins.int, builtins.float]"
589676[builtins fixtures/tuple.pyi]
677+
678+ [case testTypeVarDefaultsTypeAliasRecursive1]
679+ # flags: --disallow-any-generics
680+ from typing import Dict, List, TypeVar
681+
682+ T1 = TypeVar("T1")
683+ T2 = TypeVar("T2", default=T1)
684+
685+ TD1 = Dict[T1, T2]
686+
687+ def func_d1(
688+ a: TD1, # E: Missing type parameters for generic type "TD1"
689+ b: TD1[int],
690+ c: TD1[int, float],
691+ ) -> None:
692+ reveal_type(a) # N: Revealed type is "builtins.dict[Any, Any]"
693+ reveal_type(b) # N: Revealed type is "builtins.dict[builtins.int, builtins.int]"
694+ reveal_type(c) # N: Revealed type is "builtins.dict[builtins.int, builtins.float]"
695+ [builtins fixtures/dict.pyi]
696+
697+ [case testTypeVarDefaultsTypeAliasRecursive2]
698+ from typing import Any, Dict, Generic, TypeVar
699+
700+ T1 = TypeVar("T1", default=str)
701+ T2 = TypeVar("T2", default=T1)
702+ Alias1 = Dict[T1, T2]
703+ T3 = TypeVar("T3")
704+ class A(Generic[T3]): ...
705+
706+ T4 = TypeVar("T4", default=A[Alias1])
707+ class B(Generic[T4]): ...
708+
709+ def func_d3(
710+ a: B,
711+ b: B[A[Alias1[int]]],
712+ c: B[A[Alias1[int, float]]],
713+ d: B[int],
714+ ) -> None:
715+ reveal_type(a) # N: Revealed type is "__main__.B[__main__.A[builtins.dict[builtins.str, builtins.str]]]"
716+ reveal_type(b) # N: Revealed type is "__main__.B[__main__.A[builtins.dict[builtins.int, builtins.int]]]"
717+ reveal_type(c) # N: Revealed type is "__main__.B[__main__.A[builtins.dict[builtins.int, builtins.float]]]"
718+ reveal_type(d) # N: Revealed type is "__main__.B[builtins.int]"
719+ [builtins fixtures/dict.pyi]
0 commit comments