Skip to content
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
3 changes: 3 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4413,7 +4413,10 @@ def check_op_reversible(
)
)
)
# The data model gives reflected methods priority only for proper subclasses,
# not same-type operands (#22386).
# Note: use `covers_at_runtime` instead of `is_subtype` (#19006)
and not is_same_type(left_type, right_type)
and covers_at_runtime(right_type, left_type)
):
# When we do "A() + B()" where B is a subclass of A, we'll actually try calling
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,23 @@ assert_type(tupX + size, Size)

[builtins fixtures/tuple-typeshed.pyi]

[case testReverseBinaryOperatorSameTypeDoesNotGetPriority]
from typing import assert_type

class Base:
def __lt__(self, other: "Base") -> str: return "lt"

class Child(Base):
def __gt__(self, other: "Child") -> int: return 7

class Sub(Child): pass

a = Child()
b = Child()
assert_type(a < b, str)
assert_type(a > b, int)
assert_type(a < Sub(), int)

[case testBinaryOperatorWithAnyRightOperand]
from typing import Any, cast
class A: pass
Expand Down
Loading