Skip to content

Fix overload resolution on Tuples #1585

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,8 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
if isinstance(formal, UnionType):
return max(overload_arg_similarity(actual, item)
for item in formal.items)
if isinstance(formal, TupleType):
formal = formal.fallback
if isinstance(formal, Instance):
if isinstance(actual, CallableType):
actual = actual.fallback
Expand Down
17 changes: 17 additions & 0 deletions mypy/test/data/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,20 @@ def f(x: int) -> None: pass
def f(x: B) -> str: pass # E: Overloaded function signatures 2 and 3 overlap with incompatible return types
@overload
def f(x: A) -> int: pass

[case testOverloadTuple]
from typing import overload, Tuple
@overload
def f(x: int, y: Tuple[str, ...]) -> None: pass
@overload
def f(x: int, y: str) -> None: pass
f(1, ('2', '3'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test calling with a Tuple[str, ...] arg and a Tuple[int, ...] argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, now it's actually added.

f(1, (2, '3')) # E: Argument 2 to "f" has incompatible type "Tuple[int, str]"; expected Tuple[str, ...]
f(1, ('2',))
f(1, '2')
f(1, (2, 3)) # E: Argument 2 to "f" has incompatible type "Tuple[int, int]"; expected Tuple[str, ...]
x = ('2', '3') # type: Tuple[str, ...]
f(1, x)
y = (2, 3) # type: Tuple[int, ...]
f(1, y) # E: Argument 2 to "f" has incompatible type Tuple[int, ...]; expected Tuple[str, ...]
[builtins fixtures/tuple.py]
9 changes: 9 additions & 0 deletions mypy/test/data/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1211,3 +1211,12 @@ re.subn(spat, lambda m: '', '')[0] + ''
[out]
_program.py:7: error: Type argument 1 of "search" has incompatible value "object"
_program.py:9: error: Cannot infer type argument 1 of "search"

[case testListSetitemTuple]
from typing import List, Tuple
a = [] # type: List[Tuple[str, int]]
a[0] = 'x', 1
a[1] = 2, 'y'
a[:] = [('z', 3)]
[out]
_program.py:4: error: Incompatible types in assignment