Skip to content

Commit 4fe7d9f

Browse files
committed
Handle tuples specially
1 parent dc2868e commit 4fe7d9f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mypy/checker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7693,6 +7693,9 @@ def get_isinstance_type(self, expr: Expression) -> list[TypeRange] | None:
76937693
if isinstance(typ, FunctionLike) and typ.is_type_obj():
76947694
# If a type is generic, `isinstance` can only narrow its variables to Any.
76957695
erased_type = fill_typevars_with_any(typ.type_object())
7696+
# Tuples may have unattended type variables among their items
7697+
if isinstance(erased_type, TupleType):
7698+
erased_type = erase_typevars(erased_type)
76967699
types.append(TypeRange(erased_type, is_upper_bound=False))
76977700
elif isinstance(typ, TypeType):
76987701
# Type[A] means "any type that is a subtype of A" rather than "precisely type A"

test-data/unit/check-narrowing.test

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2480,6 +2480,7 @@ def check_a(obj: "A[T] | str") -> None:
24802480
else:
24812481
reveal_type(obj) # N: Revealed type is "builtins.str"
24822482

2483+
24832484
class B(Generic[T]):
24842485
@overload
24852486
def __init__(self, x: T) -> None: ...
@@ -2495,6 +2496,7 @@ def check_b(obj: "B[T] | str") -> None:
24952496
else:
24962497
reveal_type(obj) # N: Revealed type is "builtins.str"
24972498

2499+
24982500
class C(Generic[T]):
24992501
@overload
25002502
def __init__(self: C[int]) -> None: ...
@@ -2509,4 +2511,12 @@ def check_c(obj: "C[T] | str") -> None:
25092511
reveal_type(obj) # N: Revealed type is "__main__.C[T`-1]"
25102512
else:
25112513
reveal_type(obj) # N: Revealed type is "builtins.str"
2512-
[builtins fixtures/isinstance.pyi]
2514+
2515+
2516+
class D(tuple[T], Generic[T]): ...
2517+
2518+
def check_d(arg: D[T]) -> None:
2519+
if not isinstance(arg, D):
2520+
return
2521+
reveal_type(arg) # N: Revealed type is "tuple[T`-1, fallback=__main__.D[Any]]"
2522+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)