diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index fcec00fa239d..2394017085ad 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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 diff --git a/mypy/test/data/check-overloading.test b/mypy/test/data/check-overloading.test index bf14a433a781..e76eb009810c 100644 --- a/mypy/test/data/check-overloading.test +++ b/mypy/test/data/check-overloading.test @@ -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')) +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] diff --git a/mypy/test/data/pythoneval.test b/mypy/test/data/pythoneval.test index 604befc6c211..025803360417 100644 --- a/mypy/test/data/pythoneval.test +++ b/mypy/test/data/pythoneval.test @@ -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