diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index c48f3da51f85..b61f42820507 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -4397,7 +4397,7 @@ def check_op_reversible( # This is the case even if the __add__ method is completely missing and the __radd__ # method is defined. - variants_raw = [(op_name, left_op, left_type, right_expr)] + variants_raw = [(op_name, left_op, left_type, right_expr, False)] elif ( ( # Checking (A implies B) using the logically equivalent (not A or B), where @@ -4427,16 +4427,16 @@ def check_op_reversible( # __radd__ method of int if the LHS is a native int type. variants_raw = [ - (rev_op_name, right_op, right_type, left_expr), - (op_name, left_op, left_type, right_expr), + (rev_op_name, right_op, right_type, left_expr, True), + (op_name, left_op, left_type, right_expr, False), ] else: # In all other cases, we do the usual thing and call __add__ first and # __radd__ second when doing "A() + B()". variants_raw = [ - (op_name, left_op, left_type, right_expr), - (rev_op_name, right_op, right_type, left_expr), + (op_name, left_op, left_type, right_expr, False), + (rev_op_name, right_op, right_type, left_expr, True), ] # STEP 3: @@ -4444,7 +4444,11 @@ def check_op_reversible( # all operator methods that are actually present, in the order that Python # attempts to invoke them. - variants = [(na, op, obj, arg) for (na, op, obj, arg) in variants_raw if op is not None] + variants = [ + (na, op, obj, arg, is_reverse) + for (na, op, obj, arg, is_reverse) in variants_raw + if op is not None + ] # STEP 4: # We now try invoking each one. If an operation succeeds, end early and return @@ -4453,9 +4457,17 @@ def check_op_reversible( errors = [] results = [] - for name, method, obj, arg in variants: + for name, method, obj, arg, is_reverse in variants: with self.msg.filter_errors(save_filtered_errors=True) as local_errors: - result = self.check_method_call(name, obj, method, [arg], [ARG_POS], context) + if is_reverse: + with self.msg.reverse_operator( + context, operators.op_methods_to_symbols[op_name] + ): + result = self.check_method_call( + name, obj, method, [arg], [ARG_POS], context + ) + else: + result = self.check_method_call(name, obj, method, [arg], [ARG_POS], context) if local_errors.has_new_errors(): errors.append(local_errors.filtered_errors()) results.append(result) diff --git a/mypy/messages.py b/mypy/messages.py index 1ef5dea68e2b..e93e5f96e5e8 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -180,11 +180,17 @@ class MessageBuilder: # Hack to deduplicate error messages from union types _disable_type_names: list[bool] + # Stack of (context, operator symbol) for active reflected operator checks + # (e.g. (ComparisonExpr, ">=") when checking __le__ for >=), so error messages + # report the written operator and operand order instead of the reflected method. + _reverse_operator_contexts: list[tuple[Context, str]] + def __init__(self, errors: Errors, modules: dict[str, MypyFile]) -> None: self.errors = errors self.options = errors.options self.modules = modules self._disable_type_names = [] + self._reverse_operator_contexts = [] # # Helpers @@ -222,6 +228,19 @@ def disable_type_names(self) -> Iterator[None]: def are_type_names_disabled(self) -> bool: return len(self._disable_type_names) > 0 and self._disable_type_names[-1] + @contextmanager + def reverse_operator(self, context: Context, op: str) -> Iterator[None]: + self._reverse_operator_contexts.append((context, op)) + try: + yield + finally: + self._reverse_operator_contexts.pop() + + def get_reverse_operator(self, context: Context) -> str | None: + if self._reverse_operator_contexts and self._reverse_operator_contexts[-1][0] is context: + return self._reverse_operator_contexts[-1][1] + return None + def prefer_simple_messages(self) -> bool: """Should we generate simple/fast error messages? @@ -647,7 +666,14 @@ def incompatible_argument( for variant in method, "__r" + method[2:]: # FIX: do not rely on textual formatting if name.startswith(f'"{variant}" of'): - if op == "in" or variant != method: + reverse_op = self.get_reverse_operator(outer_context) + if reverse_op is not None: + # Reflected operator call (e.g. __le__ for >=): + # restore the original operator and operand order. + return self.unsupported_operand_types( + reverse_op, arg_type, base, context, code=codes.OPERATOR + ) + elif op == "in" or variant != method: # Reversed order of base/argument. return self.unsupported_operand_types( op, arg_type, base, context, code=codes.OPERATOR diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index 1bd955b6b732..c3f41817ff89 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -723,7 +723,78 @@ if int(): s = A() < B() if int(): n = A() < B() # E: Incompatible types in assignment (expression has type "str", variable has type "int") - s = object() < B() # E: Unsupported operand types for > ("B" and "object") + s = object() < B() # E: Unsupported operand types for < ("object" and "B") + +[case testReverseComparisonOperatorErrorMessage] +def f(x: int | None, y: int | None) -> None: + x < 0 # E: Unsupported operand types for < ("None" and "int") \ + # N: Left operand is of type "int | None" + x <= 0 # E: Unsupported operand types for <= ("None" and "int") \ + # N: Left operand is of type "int | None" + x > 0 # E: Unsupported operand types for > ("None" and "int") \ + # N: Left operand is of type "int | None" + x >= 0 # E: Unsupported operand types for >= ("None" and "int") \ + # N: Left operand is of type "int | None" + + 0 < x # E: Unsupported operand types for < ("int" and "None") \ + # N: Right operand is of type "int | None" + 0 <= x # E: Unsupported operand types for <= ("int" and "None") \ + # N: Right operand is of type "int | None" + 0 > x # E: Unsupported operand types for > ("int" and "None") \ + # N: Right operand is of type "int | None" + 0 >= x # E: Unsupported operand types for >= ("int" and "None") \ + # N: Right operand is of type "int | None" + + None < 0 # E: Unsupported operand types for < ("None" and "int") + None <= 0 # E: Unsupported operand types for <= ("None" and "int") + None > 0 # E: Unsupported operand types for > ("None" and "int") + None >= 0 # E: Unsupported operand types for >= ("None" and "int") + + 0 <= x < 10 # E: Unsupported operand types for <= ("int" and "None") \ + # N: Right operand is of type "int | None" \ + # E: Unsupported operand types for < ("None" and "int") \ + # N: Left operand is of type "int | None" + + x >= y # E: Unsupported operand types for >= ("int" and "None") \ + # E: Unsupported operand types for >= ("None" and "int") \ + # E: Unsupported left operand type for >= ("None") \ + # N: Both left and right operands are unions + + x + 0 # E: Unsupported operand types for + ("None" and "int") \ + # N: Left operand is of type "int | None" + + (0).__le__(None) # E: Unsupported operand types for <= ("int" and "None") +[builtins fixtures/ops.pyi] + +[case testReverseComparisonOperatorCustomClassErrorMessage] +class Base: + def __lt__(self, other: int) -> bool: ... + def __eq__(self, other: int) -> bool: ... # type: ignore[override] + def __ne__(self, other: int) -> bool: ... # type: ignore[override] + +class Sub(Base): + def __gt__(self, other: str) -> bool: ... + def __eq__(self, other: str) -> bool: ... # type: ignore[override] + def __ne__(self, other: str) -> bool: ... # type: ignore[override] + +class OnlyReverse: + def __lt__(self, other: int) -> bool: ... + def __le__(self, other: int) -> bool: ... + def __gt__(self, other: int) -> bool: ... + def __ge__(self, other: int) -> bool: ... + +class Plain: + pass + +Base() < Sub() # E: Unsupported operand types for < ("Base" and "Sub") +Base() == Sub() # E: Unsupported operand types for == ("Base" and "Sub") +Base() != Sub() # E: Unsupported operand types for != ("Base" and "Sub") + +Plain() < OnlyReverse() # E: Unsupported operand types for < ("Plain" and "OnlyReverse") +Plain() <= OnlyReverse() # E: Unsupported operand types for <= ("Plain" and "OnlyReverse") +Plain() > OnlyReverse() # E: Unsupported operand types for > ("Plain" and "OnlyReverse") +Plain() >= OnlyReverse() # E: Unsupported operand types for >= ("Plain" and "OnlyReverse") +[builtins fixtures/ops.pyi] [case testReversibleComparisonWithExtraArgument] class C: diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index cf1369691f08..88c8b6b57170 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -3097,10 +3097,10 @@ A(1) >= 1 # E: Unsupported operand types for >= ("A" and "int") A(1) == 1 A(1) != 1 -1 < A(1) # E: Unsupported operand types for > ("A" and "int") -1 <= A(1) # E: Unsupported operand types for >= ("A" and "int") -1 > A(1) # E: Unsupported operand types for < ("A" and "int") -1 >= A(1) # E: Unsupported operand types for <= ("A" and "int") +1 < A(1) # E: Unsupported operand types for < ("int" and "A") +1 <= A(1) # E: Unsupported operand types for <= ("int" and "A") +1 > A(1) # E: Unsupported operand types for > ("int" and "A") +1 >= A(1) # E: Unsupported operand types for >= ("int" and "A") 1 == A(1) 1 != A(1) @@ -3123,10 +3123,10 @@ main:15: error: Unsupported operand types for < ("A" and "int") main:16: error: Unsupported operand types for <= ("A" and "int") main:17: error: Unsupported operand types for > ("A" and "int") main:18: error: Unsupported operand types for >= ("A" and "int") -main:22: error: Unsupported operand types for > ("A" and "int") -main:23: error: Unsupported operand types for >= ("A" and "int") -main:24: error: Unsupported operand types for < ("A" and "int") -main:25: error: Unsupported operand types for <= ("A" and "int") +main:22: error: Unsupported operand types for < ("int" and "A") +main:23: error: Unsupported operand types for <= ("int" and "A") +main:24: error: Unsupported operand types for > ("int" and "A") +main:25: error: Unsupported operand types for >= ("int" and "A") [case testAttrsIncrementalSubclassModified] from b import B @@ -4010,10 +4010,10 @@ tmp/b.py:18: error: Unsupported operand types for < ("A" and "int") tmp/b.py:19: error: Unsupported operand types for <= ("A" and "int") tmp/b.py:20: error: Unsupported operand types for > ("A" and "int") tmp/b.py:21: error: Unsupported operand types for >= ("A" and "int") -tmp/b.py:25: error: Unsupported operand types for > ("A" and "int") -tmp/b.py:26: error: Unsupported operand types for >= ("A" and "int") -tmp/b.py:27: error: Unsupported operand types for < ("A" and "int") -tmp/b.py:28: error: Unsupported operand types for <= ("A" and "int") +tmp/b.py:25: error: Unsupported operand types for < ("int" and "A") +tmp/b.py:26: error: Unsupported operand types for <= ("int" and "A") +tmp/b.py:27: error: Unsupported operand types for > ("int" and "A") +tmp/b.py:28: error: Unsupported operand types for >= ("int" and "A") [case testIncrementalDataclassesSubclassModified] from b import B diff --git a/test-data/unit/check-plugin-attrs.test b/test-data/unit/check-plugin-attrs.test index 7d453dcf84d0..b9baf6f7ad9d 100644 --- a/test-data/unit/check-plugin-attrs.test +++ b/test-data/unit/check-plugin-attrs.test @@ -204,10 +204,10 @@ A(1) >= 1 # E: Unsupported operand types for >= ("A" and "int") A(1) == 1 A(1) != 1 -1 < A(1) # E: Unsupported operand types for > ("A" and "int") -1 <= A(1) # E: Unsupported operand types for >= ("A" and "int") -1 > A(1) # E: Unsupported operand types for < ("A" and "int") -1 >= A(1) # E: Unsupported operand types for <= ("A" and "int") +1 < A(1) # E: Unsupported operand types for < ("int" and "A") +1 <= A(1) # E: Unsupported operand types for <= ("int" and "A") +1 > A(1) # E: Unsupported operand types for > ("int" and "A") +1 >= A(1) # E: Unsupported operand types for >= ("int" and "A") 1 == A(1) 1 != A(1) [builtins fixtures/plugin_attrs.pyi]