Skip to content

Commit bdad88a

Browse files
Michael0x2ailevkivskyi
authored andcommitted
Remove the 'rop will not be called' error message (#5571)
#5475 introduced a new type of error message ("__rop__ will not be called when evaluating 'a + b'...") that triggers when the user tries evaluating expressions like `foo + foo` where `foo` does not contain an `__add__` method that accepts a value of the same type, but *does* contain an `__radd__` method that does. This pull request removes that error message on the grounds that it's too cryptic and unlikely to be helpful to most mypy users. That error message is useful mainly for people developing libraries containing custom numeric types (or libraries that appropriate operators to create custom DSLs) -- however, most people are not library creators and so will not find this error message useful.
1 parent cd6712d commit bdad88a

File tree

3 files changed

+3
-33
lines changed

3 files changed

+3
-33
lines changed

mypy/checkexpr.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,6 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
19091909
# We store the determined order inside the 'variants_raw' variable,
19101910
# which records tuples containing the method, base type, and the argument.
19111911

1912-
warn_about_uncalled_reverse_operator = False
19131912
bias_right = is_proper_subtype(right_type, left_type)
19141913
if op_name in nodes.op_methods_that_shortcut and is_same_type(left_type, right_type):
19151914
# When we do "A() + A()", for example, Python will only call the __add__ method,
@@ -1921,8 +1920,6 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
19211920
variants_raw = [
19221921
(left_op, left_type, right_expr)
19231922
]
1924-
if right_op is not None:
1925-
warn_about_uncalled_reverse_operator = True
19261923
elif (is_subtype(right_type, left_type)
19271924
and isinstance(left_type, Instance)
19281925
and isinstance(right_type, Instance)
@@ -2010,14 +2007,6 @@ def lookup_definer(typ: Instance, attr_name: str) -> Optional[str]:
20102007
return result
20112008

20122009
self.msg.add_errors(errors[0])
2013-
if warn_about_uncalled_reverse_operator:
2014-
self.msg.reverse_operator_method_never_called(
2015-
nodes.op_methods_to_symbols[op_name],
2016-
op_name,
2017-
right_type,
2018-
rev_op_name,
2019-
context,
2020-
)
20212010
if len(results) == 1:
20222011
return results[0]
20232012
else:

mypy/messages.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,22 +1003,6 @@ def overloaded_signatures_ret_specific(self, index: int, context: Context) -> No
10031003
self.fail('Overloaded function implementation cannot produce return type '
10041004
'of signature {}'.format(index), context)
10051005

1006-
def reverse_operator_method_never_called(self,
1007-
op: str,
1008-
forward_method: str,
1009-
reverse_type: Type,
1010-
reverse_method: str,
1011-
context: Context) -> None:
1012-
msg = "{rfunc} will not be called when evaluating '{cls} {op} {cls}': must define {ffunc}"
1013-
self.note(
1014-
msg.format(
1015-
op=op,
1016-
ffunc=forward_method,
1017-
rfunc=reverse_method,
1018-
cls=self.format_bare(reverse_type),
1019-
),
1020-
context=context)
1021-
10221006
def operator_method_signatures_overlap(
10231007
self, reverse_class: TypeInfo, reverse_method: str, forward_class: Type,
10241008
forward_method: str, context: Context) -> None:

test-data/unit/check-classes.test

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,8 +1616,7 @@ class B(A): pass
16161616
# Note: This is a runtime error. If we run x.__add__(y)
16171617
# where x and y are *not* the same type, Python will not try
16181618
# calling __radd__.
1619-
A() + A() # E: Unsupported operand types for + ("A" and "A") \
1620-
# N: __radd__ will not be called when evaluating 'A + A': must define __add__
1619+
A() + A() # E: Unsupported operand types for + ("A" and "A")
16211620

16221621
# Here, Python *will* call __radd__(...)
16231622
reveal_type(B() + A()) # E: Revealed type is '__main__.A'
@@ -1733,8 +1732,7 @@ class A:
17331732
def __radd__(self, other: 'A') -> int: ...
17341733

17351734
# Note: Python only tries calling __add__ and never __radd__, even though it's present
1736-
A() + A() # E: Unsupported left operand type for + ("A") \
1737-
# N: __radd__ will not be called when evaluating 'A + A': must define __add__
1735+
A() + A() # E: Unsupported left operand type for + ("A")
17381736

17391737
[case testReverseOperatorOrderingCase2]
17401738
class A:
@@ -2006,8 +2004,7 @@ reveal_type(FractionChild() + FractionChild()) # E: Revealed type is 'builtins.
20062004

20072005
# Runtime error: we try calling __add__, it doesn't match, and we don't try __radd__ since
20082006
# the LHS and the RHS are not the same.
2009-
Fraction() + Fraction() # E: Unsupported operand types for + ("Fraction" and "Fraction") \
2010-
# N: __radd__ will not be called when evaluating 'Fraction + Fraction': must define __add__
2007+
Fraction() + Fraction() # E: Unsupported operand types for + ("Fraction" and "Fraction")
20112008

20122009
[case testReverseOperatorTypeType]
20132010
from typing import TypeVar, Type

0 commit comments

Comments
 (0)