Skip to content

[mypyc] Fix allow_interpreted_subclasses not seeing subclass attrs#21013

Open
VaggelisD wants to merge 2 commits intopython:masterfrom
VaggelisD:fix-interpreted-subclass-attr
Open

[mypyc] Fix allow_interpreted_subclasses not seeing subclass attrs#21013
VaggelisD wants to merge 2 commits intopython:masterfrom
VaggelisD:fix-interpreted-subclass-attr

Conversation

@VaggelisD
Copy link
Contributor

When a compiled class with allow_interpreted_subclasses=True has methods that access self.ATTR via direct C struct slots, interpreted subclasses that override ATTR in their class __dict__ are ignored; the compiled method always reads the base class default from the slot.

The fix: In visit_get_attr for non-property attribute access, check if the instance is a mypyc-compiled type (via a new CPy_TPFLAGS_MYPYC_COMPILED tp_flags bit). If not, fall back to PyObject_GenericGetAttr which respects the MRO and finds the subclass override.

Using tp_flags rather than an exact type check ensures compiled subclasses retain fast direct struct access, while only interpreted subclasses hit the GenericGetAttr slow path.

For unboxed types (bool, int), the PyObject* result is unboxed to the expected C type.

VaggelisD and others added 2 commits March 12, 2026 15:15
…e overrides

When a compiled class with allow_interpreted_subclasses=True has methods that
access self.ATTR via direct C struct slots, interpreted subclasses that override
ATTR in their class __dict__ are ignored — the compiled method always reads the
base class default from the slot.

Fix: in visit_get_attr for non-property attribute access, check if the instance
is a mypyc-compiled type (via a new CPy_TPFLAGS_MYPYC_COMPILED tp_flags bit).
If not, fall back to PyObject_GenericGetAttr which respects the MRO and finds
the subclass override.

Using tp_flags rather than an exact type check ensures compiled subclasses
retain fast direct struct access, while only interpreted subclasses hit the
GenericGetAttr slow path.

For unboxed types (bool, int), the PyObject* result is unboxed to the expected
C type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant