diff --git a/mypy/checker.py b/mypy/checker.py index 5829b31447fe..fe9232653317 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -95,6 +95,7 @@ OperatorAssignmentStmt, OpExpr, OverloadedFuncDef, + OverloadPart, PassStmt, PromoteExpr, RaiseStmt, @@ -399,6 +400,11 @@ def __init__( # argument through various `checker` and `checkmember` functions. self._is_final_def = False + # Track when we enter an overload implementation. Some checks should not be applied + # to the implementation signature when specific overloads are available. + # Use `enter_overload_impl` to modify. + self.overload_impl_stack: list[OverloadPart] = [] + # This flag is set when we run type-check or attribute access check for the purpose # of giving a note on possibly missing "await". It is used to avoid infinite recursion. self.checking_missing_await = False @@ -660,7 +666,8 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None: if num_abstract not in (0, len(defn.items)): self.fail(message_registry.INCONSISTENT_ABSTRACT_OVERLOAD, defn) if defn.impl: - defn.impl.accept(self) + with self.enter_overload_impl(defn.impl): + defn.impl.accept(self) if not defn.is_property: self.check_overlapping_overloads(defn) if defn.type is None: @@ -684,6 +691,14 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None: self.check_explicit_override_decorator(defn, found_method_base_classes, defn.impl) self.check_inplace_operator_method(defn) + @contextmanager + def enter_overload_impl(self, impl: OverloadPart) -> Iterator[None]: + self.overload_impl_stack.append(impl) + try: + yield + finally: + assert self.overload_impl_stack.pop() == impl + def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> CallableType | None: """Get type as seen by an overload item caller.""" inner_type = get_proper_type(inner_type) @@ -1203,7 +1218,11 @@ def check_func_def( ) if name: # Special method names - if defn.info and self.is_reverse_op_method(name): + if ( + defn.info + and self.is_reverse_op_method(name) + and defn not in self.overload_impl_stack + ): self.check_reverse_op_method(item, typ, name, defn) elif name in ("__getattr__", "__getattribute__"): self.check_getattr_method(typ, defn, name) diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index d1c33c4729a9..ab59abfb26f4 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -2487,6 +2487,60 @@ reveal_type(Num3() + Num1()) # N: Revealed type is "__main__.Num3" reveal_type(Num2() + Num3()) # N: Revealed type is "__main__.Num2" reveal_type(Num3() + Num2()) # N: Revealed type is "__main__.Num3" +[case testReverseOperatorWithOverloads3] +from typing import Union, overload + +class A: + def __mul__(self, value: A, /) -> A: ... + def __rmul__(self, value: A, /) -> A: ... + +class B: + @overload + def __mul__(self, other: B, /) -> B: ... + @overload + def __mul__(self, other: A, /) -> str: ... + def __mul__(self, other: Union[B, A], /) -> Union[B, str]: pass + + @overload + def __rmul__(self, other: B, /) -> B: ... + @overload + def __rmul__(self, other: A, /) -> str: ... + def __rmul__(self, other: Union[B, A], /) -> Union[B, str]: pass + +[case testReverseOperatorWithOverloadsNested] +from typing import Union, overload + +class A: + def __mul__(self, value: A, /) -> A: ... + def __rmul__(self, value: A, /) -> A: ... + +class B: + @overload + def __mul__(self, other: B, /) -> B: ... + @overload + def __mul__(self, other: A, /) -> str: ... + def __mul__(self, other: Union[B, A], /) -> Union[B, str]: pass + + @overload + def __rmul__(self, other: B, /) -> B: ... + @overload + def __rmul__(self, other: A, /) -> str: ... + def __rmul__(self, other: Union[B, A], /) -> Union[B, str]: + class A1: + def __add__(self, other: C1) -> int: ... + + class B1: + def __add__(self, other: C1) -> int: ... + + class C1: + @overload + def __radd__(self, other: A1) -> str: ... # E: Signatures of "__radd__" of "C1" and "__add__" of "A1" are unsafely overlapping + @overload + def __radd__(self, other: B1) -> str: ... # E: Signatures of "__radd__" of "C1" and "__add__" of "B1" are unsafely overlapping + def __radd__(self, other): pass + + return "" + [case testDivReverseOperator] # No error: __div__ has no special meaning in Python 3 class A1: