Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle TypeVarTupleType when checking overload constraints #16428

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, (TypeVarType, TypeVarTupleType)):
ilevkivskyi marked this conversation as resolved.
Show resolved Hide resolved
# Cannot infer anything for T from [T, ...] <: *Ts
continue
assert (
Expand Down
22 changes: 22 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,28 @@ 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")


ilevkivskyi marked this conversation as resolved.
Show resolved Hide resolved
@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:
...


ilevkivskyi marked this conversation as resolved.
Show resolved Hide resolved
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