Skip to content

Commit

Permalink
Handle TypeVarTupleType when checking overload constraints (#16428)
Browse files Browse the repository at this point in the history
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 <levkivskyi@gmail.com>
  • Loading branch information
2 people authored and JukkaL committed Nov 10, 2023
1 parent 8813968 commit c22294a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
for item in actual.items:
if isinstance(item, UnpackType):
unpacked = get_proper_type(item.type)
if isinstance(unpacked, TypeVarType):
if isinstance(unpacked, TypeVarTupleType):
# Cannot infer anything for T from [T, ...] <: *Ts
continue
assert (
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,24 @@ def test(a: Container[Any], b: Container[int], c: Container[str]):
reveal_type(build(b, c)) # N: Revealed type is "__main__.Array[builtins.int, builtins.str]"
[builtins fixtures/tuple.pyi]

[case testTypeVarTupleOverloadArbitraryLength]
from typing import Any, Tuple, TypeVar, TypeVarTuple, Unpack, overload

T = TypeVar("T")
Ts = TypeVarTuple("Ts")
@overload
def add(self: Tuple[Unpack[Ts]], other: Tuple[T]) -> Tuple[Unpack[Ts], T]:
...
@overload
def add(self: Tuple[T, ...], other: Tuple[T, ...]) -> Tuple[T, ...]:
...
def add(self: Any, other: Any) -> Any:
...
def test(a: Tuple[int, str], b: Tuple[bool], c: Tuple[bool, ...]):
reveal_type(add(a, b)) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.bool]"
reveal_type(add(b, c)) # N: Revealed type is "builtins.tuple[builtins.bool, ...]"
[builtins fixtures/tuple.pyi]

[case testTypeVarTupleIndexOldStyleNonNormalizedAndNonLiteral]
from typing import Any, Tuple
from typing_extensions import Unpack
Expand Down

0 comments on commit c22294a

Please sign in to comment.