Skip to content

Commit cd75c4e

Browse files
rheardp-sawicki
andauthored
[mypyc] Use other arg instead of self for RHS type (#21569)
Fixes #21568 In `gen_glue_ne_method`, mypyc currently chooses the generated RHS argument type like this when strict dunder typing is enabled: ```python rhs_type = eq_sig.args[0].type if strict_typing else object_rprimitive ``` However, the `__eq__` signature includes `self`, so `eq_sig.args[0]` is the type of `self`, not the type of `other`. --------- Co-authored-by: Piotr Sawicki <[email protected]>
1 parent 70b74e1 commit cd75c4e

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎mypyc/irbuild/classdef.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ def gen_glue_ne_method(builder: IRBuilder, cls: ClassIR, line: int) -> None:
865865
eq_sig = cls.method_sig("__eq__")
866866
strict_typing = builder.options.strict_dunders_typing
867867
with builder.enter_method(cls, "__ne__", eq_sig.ret_type):
868-
rhs_type = eq_sig.args[0].type if strict_typing else object_rprimitive
868+
rhs_type = eq_sig.args[1].type
869869
rhs_arg = builder.add_argument("rhs", rhs_type)
870870
eqval = builder.add(MethodCall(builder.self(), "__eq__", [rhs_arg], line))
871871

‎mypyc/test-data/run-dunders.test‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,3 +1009,15 @@ def test_equality_with_implicit_ne() -> None:
10091009
assert not eq(Eq(1), Eq(2))
10101010
assert ne(Eq(1), Eq(2))
10111011
assert not ne(Eq(1), Eq(1))
1012+
1013+
[case testDundersImplicitNeWithNarrowRhs]
1014+
class Accepted:
1015+
pass
1016+
1017+
class Eq:
1018+
def __eq__(self, other: Accepted) -> bool: # type: ignore[override]
1019+
return True
1020+
1021+
def test_implicit_ne_with_narrow_rhs() -> None:
1022+
assert Eq() == Accepted()
1023+
assert not (Eq() != Accepted())

0 commit comments

Comments
 (0)