Skip to content

Commit 80eb33f

Browse files
committed
Update the overlapping check for tuples to account for NamedTuples
1 parent c08719d commit 80eb33f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

mypy/meet.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
find_unpack_in_list,
5151
get_proper_type,
5252
get_proper_types,
53+
is_named_instance,
5354
split_with_prefix_and_suffix,
5455
)
5556

@@ -645,7 +646,18 @@ def are_tuples_overlapping(
645646

646647
if len(left.items) != len(right.items):
647648
return False
648-
return all(is_overlapping(l, r) for l, r in zip(left.items, right.items))
649+
if not all(is_overlapping(l, r) for l, r in zip(left.items, right.items)):
650+
return False
651+
652+
# Check that the tuples aren't from e.g. different NamedTuples.
653+
if is_named_instance(right.partial_fallback, "builtins.tuple") or is_named_instance(
654+
left.partial_fallback, "builtins.tuple"
655+
):
656+
return True
657+
else:
658+
return is_subtype(left.partial_fallback, right.partial_fallback) or is_subtype(
659+
right.partial_fallback, left.partial_fallback
660+
)
649661

650662

651663
def expand_tuple_if_possible(tup: TupleType, target: int) -> TupleType:

test-data/unit/check-namedtuple.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,3 +1474,19 @@ def main(n: NT[T]) -> None:
14741474

14751475
[builtins fixtures/tuple.pyi]
14761476
[typing fixtures/typing-namedtuple.pyi]
1477+
1478+
[case testNamedTupleOverlappingCheck]
1479+
from typing import overload, NamedTuple, Union
1480+
1481+
class A(NamedTuple): ...
1482+
1483+
class B(NamedTuple): ...
1484+
1485+
@overload
1486+
def f(arg: A) -> A: ...
1487+
@overload
1488+
def f(arg: B) -> B: ...
1489+
def f(arg: Union[A, B]) -> Union[A, B]: ...
1490+
1491+
# no errors should be raised above.
1492+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)