Skip to content

Commit ab2a7b2

Browse files
committed
Fix overload resolution on Tuples
Fixes python#1578, since list's __setitem__ is overloaded.
1 parent 04f7965 commit ab2a7b2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

mypy/checkexpr.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,8 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
16821682
if isinstance(formal, UnionType):
16831683
return max(overload_arg_similarity(actual, item)
16841684
for item in formal.items)
1685+
if isinstance(formal, TupleType):
1686+
formal = formal.fallback
16851687
if isinstance(formal, Instance):
16861688
if isinstance(actual, CallableType):
16871689
actual = actual.fallback

mypy/test/data/check-overloading.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,3 +678,20 @@ def f(x: int) -> None: pass
678678
def f(x: B) -> str: pass # E: Overloaded function signatures 2 and 3 overlap with incompatible return types
679679
@overload
680680
def f(x: A) -> int: pass
681+
682+
[case testOverloadTuple]
683+
from typing import overload, Tuple
684+
@overload
685+
def f(x: int, y: Tuple[str, ...]) -> None: pass
686+
@overload
687+
def f(x: int, y: str) -> None: pass
688+
f(1, ('2', '3'))
689+
f(1, (2, '3')) # E: Argument 2 to "f" has incompatible type "Tuple[int, str]"; expected Tuple[str, ...]
690+
f(1, ('2',))
691+
f(1, '2')
692+
f(1, (2, 3)) # E: Argument 2 to "f" has incompatible type "Tuple[int, int]"; expected Tuple[str, ...]
693+
x = ('2', '3') # type: Tuple[str, ...]
694+
f(1, x)
695+
y = (2, 3) # type: Tuple[int, ...]
696+
f(1, y) # E: Argument 2 to "f" has incompatible type Tuple[int, ...]; expected Tuple[str, ...]
697+
[builtins fixtures/tuple.py]

mypy/test/data/pythoneval.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,3 +1211,12 @@ re.subn(spat, lambda m: '', '')[0] + ''
12111211
[out]
12121212
_program.py:7: error: Type argument 1 of "search" has incompatible value "object"
12131213
_program.py:9: error: Cannot infer type argument 1 of "search"
1214+
1215+
[case testListSetitemTuple]
1216+
from typing import List, Tuple
1217+
a = [] # type: List[Tuple[str, int]]
1218+
a[0] = 'x', 1
1219+
a[1] = 2, 'y'
1220+
a[:] = [('z', 3)]
1221+
[out]
1222+
_program.py:4: error: Incompatible types in assignment

0 commit comments

Comments
 (0)