From eebf471169dd9c939661b0086295f8c66eb57f39 Mon Sep 17 00:00:00 2001 From: Ryan Heard Date: Sat, 30 May 2026 00:49:00 -0400 Subject: [PATCH] Use `other` arg instead of `self` for RHS type --- mypyc/irbuild/classdef.py | 2 +- mypyc/test-data/run-dunders.test | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index f5d094d142317..243b860da617e 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -867,7 +867,7 @@ def gen_glue_ne_method(builder: IRBuilder, cls: ClassIR, line: int) -> None: eq_sig = func_ir.decl.sig strict_typing = builder.options.strict_dunders_typing with builder.enter_method(cls, "__ne__", eq_sig.ret_type): - rhs_type = eq_sig.args[0].type if strict_typing else object_rprimitive + rhs_type = eq_sig.args[1].type if strict_typing else object_rprimitive rhs_arg = builder.add_argument("rhs", rhs_type) eqval = builder.add(MethodCall(builder.self(), "__eq__", [rhs_arg], line)) diff --git a/mypyc/test-data/run-dunders.test b/mypyc/test-data/run-dunders.test index a3ec06763d75b..d5907d959ccb1 100644 --- a/mypyc/test-data/run-dunders.test +++ b/mypyc/test-data/run-dunders.test @@ -1009,3 +1009,15 @@ def test_equality_with_implicit_ne() -> None: assert not eq(Eq(1), Eq(2)) assert ne(Eq(1), Eq(2)) assert not ne(Eq(1), Eq(1)) + +[case testDundersImplicitNeWithNarrowRhs] +class Accepted: + pass + +class Eq: + def __eq__(self, other: Accepted) -> bool: # type: ignore[override] + return True + +def test_implicit_ne_with_narrow_rhs() -> None: + assert Eq() == Accepted() + assert not (Eq() != Accepted())