Skip to content

Commit c22294a

Browse files
robjhornbyilevkivskyi
authored andcommitted
Handle TypeVarTupleType when checking overload constraints (#16428)
Fixes #16427 The test case added in the first commit crashes. The second commit addresses the crash - I don't know whether this fix is correct, it just happens to stop the crash but it leads to a code branch which just `continue`s out of a for loop iteration, so it might be bypassing something it shouldn't. I don't completely understand it. --------- Co-authored-by: Ivan Levkivskyi <[email protected]>
1 parent 8813968 commit c22294a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

mypy/constraints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
949949
for item in actual.items:
950950
if isinstance(item, UnpackType):
951951
unpacked = get_proper_type(item.type)
952-
if isinstance(unpacked, TypeVarType):
952+
if isinstance(unpacked, TypeVarTupleType):
953953
# Cannot infer anything for T from [T, ...] <: *Ts
954954
continue
955955
assert (

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,24 @@ def test(a: Container[Any], b: Container[int], c: Container[str]):
17891789
reveal_type(build(b, c)) # N: Revealed type is "__main__.Array[builtins.int, builtins.str]"
17901790
[builtins fixtures/tuple.pyi]
17911791

1792+
[case testTypeVarTupleOverloadArbitraryLength]
1793+
from typing import Any, Tuple, TypeVar, TypeVarTuple, Unpack, overload
1794+
1795+
T = TypeVar("T")
1796+
Ts = TypeVarTuple("Ts")
1797+
@overload
1798+
def add(self: Tuple[Unpack[Ts]], other: Tuple[T]) -> Tuple[Unpack[Ts], T]:
1799+
...
1800+
@overload
1801+
def add(self: Tuple[T, ...], other: Tuple[T, ...]) -> Tuple[T, ...]:
1802+
...
1803+
def add(self: Any, other: Any) -> Any:
1804+
...
1805+
def test(a: Tuple[int, str], b: Tuple[bool], c: Tuple[bool, ...]):
1806+
reveal_type(add(a, b)) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.bool]"
1807+
reveal_type(add(b, c)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
1808+
[builtins fixtures/tuple.pyi]
1809+
17921810
[case testTypeVarTupleIndexOldStyleNonNormalizedAndNonLiteral]
17931811
from typing import Any, Tuple
17941812
from typing_extensions import Unpack

0 commit comments

Comments
 (0)