Skip to content

Commit e880af6

Browse files
committed
more tuple test
1 parent 9c2e697 commit e880af6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test-data/unit/check-typeis.test

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,54 @@ def f4(a: Union[List[int], int]) -> None:
171171
reveal_type(a) # N: Revealed type is "Union[builtins.list[builtins.int], builtins.int]"
172172
[builtins fixtures/tuple.pyi]
173173

174+
[case testTypeIsTupleGeneric]
175+
# flags: --warn-unreachable
176+
from __future__ import annotations
177+
from typing_extensions import TypeIs
178+
179+
class A: ...
180+
class B: ...
181+
182+
def is_tuple_of_B(v: tuple[A | B, ...]) -> TypeIs[tuple[B, ...]]: ...
183+
184+
def test1(t: tuple[A]) -> None:
185+
if is_tuple_of_B(t):
186+
reveal_type(t) # E: Statement is unreachable
187+
else:
188+
reveal_type(t) # N: Revealed type is "tuple[__main__.A]"
189+
190+
def test2(t: tuple[B, A]) -> None:
191+
if is_tuple_of_B(t):
192+
reveal_type(t) # E: Statement is unreachable
193+
else:
194+
reveal_type(t) # N: Revealed type is "tuple[__main__.B, __main__.A]"
195+
196+
def test3(t: tuple[A | B]) -> None:
197+
if is_tuple_of_B(t):
198+
reveal_type(t) # N: Revealed type is "tuple[__main__.B]"
199+
else:
200+
reveal_type(t) # N: Revealed type is "tuple[Union[__main__.A, __main__.B]]"
201+
202+
def test4(t: tuple[A | B, A | B]) -> None:
203+
if is_tuple_of_B(t):
204+
reveal_type(t) # N: Revealed type is "tuple[__main__.B, __main__.B]"
205+
else:
206+
reveal_type(t) # N: Revealed type is "tuple[Union[__main__.A, __main__.B], Union[__main__.A, __main__.B]]"
207+
208+
def test5(t: tuple[A | B, ...]) -> None:
209+
if is_tuple_of_B(t):
210+
reveal_type(t) # N: Revealed type is "builtins.tuple[__main__.B, ...]"
211+
else:
212+
reveal_type(t) # N: Revealed type is "builtins.tuple[Union[__main__.A, __main__.B], ...]"
213+
214+
def test6(t: tuple[B, *tuple[A | B, ...], B]) -> None:
215+
if is_tuple_of_B(t):
216+
# Should this be tuple[B, *tuple[B, ...], B]
217+
reveal_type(t) # N: Revealed type is "tuple[__main__.B, Never, __main__.B]"
218+
else:
219+
reveal_type(t) # N: Revealed type is "tuple[__main__.B, Unpack[builtins.tuple[Union[__main__.A, __main__.B], ...]], __main__.B]"
220+
[builtins fixtures/tuple.pyi]
221+
174222
[case testTypeIsNonzeroFloat]
175223
from typing_extensions import TypeIs
176224
def is_nonzero(a: object) -> TypeIs[float]: pass

0 commit comments

Comments
 (0)