Skip to content

Commit a871b87

Browse files
committed
Add basic usage to testcases, add test for inline tvar defaults
1 parent c7b9670 commit a871b87

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

test-data/unit/check-python312.test

+12
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,7 @@ b: tuple[*tuple[int, ...], *tuple[int, ...]] # E: More than one Unpack in a typ
20442044
[typing fixtures/typing-full.pyi]
20452045

20462046
[case testPEP695TypeVarConstraintsDefaultAliases]
2047+
from typing import Generic
20472048
from typing_extensions import TypeVar
20482049

20492050
type K = int
@@ -2053,4 +2054,15 @@ type L = list[int]
20532054
T1 = TypeVar("T1", str, K, default=K)
20542055
T2 = TypeVar("T2", str, K, default=V)
20552056
T3 = TypeVar("T3", str, L, default=L)
2057+
2058+
class A1(Generic[T1]):
2059+
x: T1
2060+
class A2(Generic[T2]):
2061+
x: T2
2062+
class A3(Generic[T3]):
2063+
x: T3
2064+
2065+
reveal_type(A1().x) # N: Revealed type is "builtins.int"
2066+
reveal_type(A2().x) # N: Revealed type is "builtins.int"
2067+
reveal_type(A3().x) # N: Revealed type is "builtins.list[builtins.int]"
20562068
[builtins fixtures/tuple.pyi]

test-data/unit/check-python313.test

+16
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,19 @@ def func_c1(
255255

256256
[builtins fixtures/tuple.pyi]
257257
[typing fixtures/typing-full.pyi]
258+
259+
[case testTypeVarConstraintsDefaultAliasesInline]
260+
type K = int
261+
type V = int
262+
263+
class A1[T: (str, int) = K]:
264+
x: T
265+
class A2[T: (str, K) = K]:
266+
x: T
267+
class A3[T: (str, K) = V]:
268+
x: T
269+
270+
reveal_type(A1().x) # N: Revealed type is "builtins.int"
271+
reveal_type(A2().x) # N: Revealed type is "builtins.int"
272+
reveal_type(A3().x) # N: Revealed type is "builtins.int"
273+
[builtins fixtures/tuple.pyi]

test-data/unit/check-typevar-defaults.test

+32-1
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ reveal_type(a.a) # N: Revealed type is "Union[builtins.int, None]"
749749
[builtins fixtures/tuple.pyi]
750750

751751
[case testTypeVarConstraintsDefaultAliasesTypeAliasType]
752+
from typing import Generic
752753
from typing_extensions import TypeAliasType, TypeVar
753754

754755
K = TypeAliasType("K", int)
@@ -757,6 +758,17 @@ L = TypeAliasType("L", list[int])
757758
T1 = TypeVar("T1", str, K, default=K)
758759
T2 = TypeVar("T2", str, K, default=V)
759760
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]"
760772
[builtins fixtures/tuple.pyi]
761773

762774
[case testTypeVarConstraintsDefaultAliasesImplicitAlias]
@@ -782,7 +794,7 @@ T3 = TypeVar("T3", str, L, default=L)
782794
[builtins fixtures/tuple.pyi]
783795

784796
[case testTypeVarConstraintsDefaultSpecialTypes]
785-
from typing import NamedTuple
797+
from typing import Generic, NamedTuple
786798
from typing_extensions import TypedDict, TypeVar
787799

788800
class TD(TypedDict):
@@ -793,6 +805,14 @@ class NT(NamedTuple):
793805

794806
T1 = TypeVar("T1", str, TD, default=TD)
795807
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]"
796816
[builtins fixtures/tuple.pyi]
797817

798818
[case testTypeVarConstraintsDefaultSpecialTypesGeneric]
@@ -816,4 +836,15 @@ T2 = TypeVar("T2", str, NT[int], default=NT[int])
816836
T3 = TypeVar("T3", str, TD2, default=TD[int])
817837
T4 = TypeVar("T4", str, TD3, default=TD[int]) # E: TypeVar default must be one of the constraint types
818838
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})"
819850
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)