Skip to content

Ignore overload impl when checking __OP__ and __rOP__ compatibility #18502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 21 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
OperatorAssignmentStmt,
OpExpr,
OverloadedFuncDef,
OverloadPart,
PassStmt,
PromoteExpr,
RaiseStmt,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading