Skip to content

Commit 44a9687

Browse files
types overlap only if all type args match (#9452)
Fixes #9451
1 parent 37777b3 commit 44a9687

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

mypy/meet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ def _type_object_overlap(left: Type, right: Type) -> bool:
339339
# Or, to use a more concrete example, List[Union[A, B]] and List[Union[B, C]]
340340
# would be considered partially overlapping since it's possible for both lists
341341
# to contain only instances of B at runtime.
342-
for left_arg, right_arg in zip(left.args, right.args):
343-
if _is_overlapping_types(left_arg, right_arg):
344-
return True
342+
if all(_is_overlapping_types(left_arg, right_arg)
343+
for left_arg, right_arg in zip(left.args, right.args)):
344+
return True
345345

346346
return False
347347

test-data/unit/check-overloading.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5151,3 +5151,25 @@ compose(ID, fakeint)("test")
51515151
reveal_type(compose(ID, fakeint)) # N: Revealed type is 'def (Union[builtins.str, builtins.bytes]) -> __main__.ID*'
51525152

51535153
[builtins fixtures/tuple.pyi]
5154+
5155+
[case testOverloadTwoTypeArgs]
5156+
from typing import Generic, overload, TypeVar, Any
5157+
5158+
T1 = TypeVar("T1")
5159+
T2 = TypeVar("T2")
5160+
5161+
class A: ...
5162+
class B: ...
5163+
class G(Generic[T1, T2]): ...
5164+
5165+
@overload
5166+
def f1(g: G[A, A]) -> A: ...
5167+
@overload
5168+
def f1(g: G[A, B]) -> B: ...
5169+
def f1(g: Any) -> Any: ...
5170+
5171+
@overload
5172+
def f2(g: G[A, Any]) -> A: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
5173+
@overload
5174+
def f2(g: G[A, B], x: int = ...) -> B: ...
5175+
def f2(g: Any, x: int = ...) -> Any: ...

0 commit comments

Comments
 (0)