Skip to content

Do not cache lookups that skip non-static bases in the shared MRO attribute assumptions - #1394

Open
taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH-mro-cache-skip-non-static-bases
Open

taxor03 wants to merge 1 commit into
oracle:masterfrom
taxor03:GH-mro-cache-skip-non-static-bases

Conversation

@taxor03

@taxor03 taxor03 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #1342

LookupAttributeInMRONode.lookupConstantMROCached gets its value from findAttrAndAssumptionInMRO, which stores the result in the MRO's final attribute assumption for the key. Every lookup of that key on the class shares it. A node created with createForLookupOfUnmanagedClasses skips the non-static bases, so its result can differ from a normal lookup, but it went through the same specialization and stored or read the same entry.

__class__ assignment uses such a node for __new__ (CheckCompatibleForAssigmentNode). After obj.__class__ = C, the shared entry for __new__ on C held object.__new__, and normal lookups returned it. C(arg) for a class with a non-default metatype (which gets __new__ with PyObject_GetAttr) then called object.__new__ with the argument and raised TypeError. The other way round, after type.__getattribute__(C, "__new__"), the compatibility check read Base.__new__ for C from the shared entry but object.__new__ for Base, and Base().__class__ = C raised "__class__ assignment: 'C' object layout differs from 'Base'".

AS-IS

>>> class Meta(type): pass
>>> class Base:
...     def __new__(cls, *args): return object.__new__(cls)
>>> class C(Base, metaclass=Meta): pass
>>> Base().__class__ = C
>>> type.__getattribute__(C, "__new__")
<built-in method __new__ of type object at 0x...>

TO-BE

<function Base.__new__ at 0x...>

(matching CPython)

Changes

  • Take lookupConstantMROCached only for nodes that do not skip non-static bases. Nodes that skip them use the slow path, whose caches belong to the node.
  • Add test_class_assignment_keeps_new_lookup to tests/test_mro.py: after __class__ assignment to a class with a non-default metatype, __new__ lookups and instance creation keep finding the base's __new__, and type.__getattribute__(D, "__new__") before an assignment does not make it fail. Each half fails on its own without the change. It passes on CPython 3.12, 3.13 and 3.14.

Testing

mx build on darwin-aarch64 (master 3176851), then mx graalpytest test_mro (12 passed) and --tagged test_descr (134 passed, 2 expected failures), test_abc (72), test_class (19) and test_types (107). The reproducer in #1342 prints C for all four rounds. Without the change, the new test fails at the first type.__getattribute__(C, "__new__"). mx checkstyle --primary and mx eclipseformat --primary report nothing.

…ribute assumptions

Signed-off-by: Sungmin Park <ryan040@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

1 participant