@@ -729,8 +729,6 @@ class C(Generic[_I]): pass
729
729
t: type[C] | int = C
730
730
[builtins fixtures/tuple.pyi]
731
731
732
-
733
-
734
732
[case testGenericTypeAliasWithDefaultTypeVarPreservesNoneInDefault]
735
733
from typing_extensions import TypeVar
736
734
from typing import Generic, Union
@@ -749,3 +747,104 @@ MyA = A[T1, int]
749
747
a: MyA = A(None, 10)
750
748
reveal_type(a.a) # N: Revealed type is "Union[builtins.int, None]"
751
749
[builtins fixtures/tuple.pyi]
750
+
751
+ [case testTypeVarConstraintsDefaultAliasesTypeAliasType]
752
+ from typing import Generic
753
+ from typing_extensions import TypeAliasType, TypeVar
754
+
755
+ K = TypeAliasType("K", int)
756
+ V = TypeAliasType("V", int)
757
+ L = TypeAliasType("L", list[int])
758
+ T1 = TypeVar("T1", str, K, default=K)
759
+ T2 = TypeVar("T2", str, K, default=V)
760
+ T3 = TypeVar("T3", str, L, default=L)
761
+
762
+ class A1(Generic[T1]):
763
+ x: T1
764
+ class A2(Generic[T2]):
765
+ x: T2
766
+ class A3(Generic[T3]):
767
+ x: T3
768
+
769
+ reveal_type(A1().x) # N: Revealed type is "builtins.int"
770
+ reveal_type(A2().x) # N: Revealed type is "builtins.int"
771
+ reveal_type(A3().x) # N: Revealed type is "builtins.list[builtins.int]"
772
+ [builtins fixtures/tuple.pyi]
773
+
774
+ [case testTypeVarConstraintsDefaultAliasesImplicitAlias]
775
+ from typing_extensions import TypeVar
776
+
777
+ K = int
778
+ V = int
779
+ L = list[int]
780
+ T1 = TypeVar("T1", str, K, default=K)
781
+ T2 = TypeVar("T2", str, K, default=V)
782
+ T3 = TypeVar("T3", str, L, default=L)
783
+ [builtins fixtures/tuple.pyi]
784
+
785
+ [case testTypeVarConstraintsDefaultAliasesExplicitAlias]
786
+ from typing_extensions import TypeAlias, TypeVar
787
+
788
+ K: TypeAlias = int
789
+ V: TypeAlias = int
790
+ L: TypeAlias = list[int]
791
+ T1 = TypeVar("T1", str, K, default=K)
792
+ T2 = TypeVar("T2", str, K, default=V)
793
+ T3 = TypeVar("T3", str, L, default=L)
794
+ [builtins fixtures/tuple.pyi]
795
+
796
+ [case testTypeVarConstraintsDefaultSpecialTypes]
797
+ from typing import Generic, NamedTuple
798
+ from typing_extensions import TypedDict, TypeVar
799
+
800
+ class TD(TypedDict):
801
+ foo: str
802
+
803
+ class NT(NamedTuple):
804
+ foo: str
805
+
806
+ T1 = TypeVar("T1", str, TD, default=TD)
807
+ T2 = TypeVar("T2", str, NT, default=NT)
808
+
809
+ class A1(Generic[T1]):
810
+ x: T1
811
+ class A2(Generic[T2]):
812
+ x: T2
813
+
814
+ reveal_type(A1().x) # N: Revealed type is "TypedDict('__main__.TD', {'foo': builtins.str})"
815
+ reveal_type(A2().x) # N: Revealed type is "tuple[builtins.str, fallback=__main__.NT]"
816
+ [builtins fixtures/tuple.pyi]
817
+
818
+ [case testTypeVarConstraintsDefaultSpecialTypesGeneric]
819
+ from typing import Generic, NamedTuple
820
+ from typing_extensions import TypedDict, TypeVar
821
+
822
+ T = TypeVar("T")
823
+
824
+ class TD(TypedDict, Generic[T]):
825
+ foo: T
826
+ class TD2(TD[int]): pass
827
+ class TD3(TD[int]):
828
+ bar: str
829
+
830
+ class NT(NamedTuple, Generic[T]):
831
+ foo: T
832
+ class NT2(NT[int]): pass
833
+
834
+ T1 = TypeVar("T1", str, TD[int], default=TD[int])
835
+ T2 = TypeVar("T2", str, NT[int], default=NT[int])
836
+ T3 = TypeVar("T3", str, TD2, default=TD[int])
837
+ T4 = TypeVar("T4", str, TD3, default=TD[int]) # E: TypeVar default must be one of the constraint types
838
+ T5 = TypeVar("T5", str, NT2, default=NT[int]) # E: TypeVar default must be one of the constraint types
839
+
840
+ class A1(Generic[T1]):
841
+ x: T1
842
+ class A2(Generic[T2]):
843
+ x: T2
844
+ class A3(Generic[T3]):
845
+ x: T3
846
+
847
+ reveal_type(A1().x) # N: Revealed type is "TypedDict('__main__.TD', {'foo': builtins.int})"
848
+ reveal_type(A2().x) # N: Revealed type is "tuple[builtins.int, fallback=__main__.NT[builtins.int]]"
849
+ reveal_type(A3().x) # N: Revealed type is "TypedDict('__main__.TD', {'foo': builtins.int})"
850
+ [builtins fixtures/tuple.pyi]
0 commit comments