Skip to content

Commit 70b74e1

Browse files
authored
[mypyc] Use method_sig to get the method signature (#21567)
Fixes #21566 `create_ne_from_eq()` checks whether the class has an `__eq__` method using `cls.has_method("__eq__")`, but `gen_glue_ne_method()` then requires `cls.get_method("__eq__")` to return a concrete `FuncIR`. In this package-cycle case, the inherited method declaration exists, but the concrete `FuncIR` body is not available yet. This makes `cls.has_method("__eq__")` true while `cls.get_method("__eq__")` returns None.
1 parent 8f2d0f1 commit 70b74e1

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎mypyc/irbuild/classdef.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,7 @@ def create_ne_from_eq(builder: IRBuilder, cdef: ClassDef) -> None:
862862

863863
def gen_glue_ne_method(builder: IRBuilder, cls: ClassIR, line: int) -> None:
864864
"""Generate a "__ne__" method from a "__eq__" method."""
865-
func_ir = cls.get_method("__eq__")
866-
assert func_ir
867-
eq_sig = func_ir.decl.sig
865+
eq_sig = cls.method_sig("__eq__")
868866
strict_typing = builder.options.strict_dunders_typing
869867
with builder.enter_method(cls, "__ne__", eq_sig.ret_type):
870868
rhs_type = eq_sig.args[0].type if strict_typing else object_rprimitive

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,41 @@ assert sum_range(0) == 0
17101710
assert sum_range(1) == 0
17111711
assert sum_range(5) == 10
17121712

1713+
[case testPackageCycleInheritedEq]
1714+
-- Regression test for generating __ne__ from an inherited __eq__ when the
1715+
-- inherited method's declaration is available before its FuncIR body has been
1716+
-- generated. This can happen in package import cycles where __init__ imports a
1717+
-- child module before the subpackage that defines the base class.
1718+
from other.other_child import Child
1719+
1720+
def make_child() -> Child:
1721+
return Child()
1722+
1723+
[file other/__init__.py]
1724+
from other.other_child import Child as Child
1725+
from other.other_subpkg import Base as Base
1726+
1727+
[file other/other_child.py]
1728+
from other.other_subpkg import Base
1729+
1730+
class Child(Base):
1731+
pass
1732+
1733+
[file other/other_subpkg/__init__.py]
1734+
from other.other_subpkg.other_base import Base as Base
1735+
1736+
[file other/other_subpkg/other_base.py]
1737+
class Base:
1738+
def __eq__(self, other: object) -> bool:
1739+
return True
1740+
1741+
[file driver.py]
1742+
from native import make_child
1743+
left = make_child()
1744+
right = make_child()
1745+
assert left == right
1746+
assert not (left != right)
1747+
17131748
[case testSeparateCrossGroupInheritedInit]
17141749
-- Under separate=True, a subclass whose __init__ is inherited from a
17151750
-- different group must call the base's CPyPy_ wrapper through the exports

0 commit comments

Comments
 (0)