Skip to content

[mypyc] Preserve inherited attribute defaults under separate=True#21547

Open
georgesittas wants to merge 1 commit into
python:masterfrom
VaggelisD:fix/mypyc-defaults-setup-cache-load
Open

[mypyc] Preserve inherited attribute defaults under separate=True#21547
georgesittas wants to merge 1 commit into
python:masterfrom
VaggelisD:fix/mypyc-defaults-setup-cache-load

Conversation

@georgesittas
Copy link
Copy Markdown
Contributor

@georgesittas georgesittas commented May 27, 2026

Fixes #21542

Under separate=True, when a subclass is recompiled while its parent is loaded from mypy's incremental cache, parent default-attribute assignments are silently dropped from the subclass's __mypyc_defaults_setup. The first read of an inherited default-attr then raises:

    AttributeError: attribute '<name>' of '<Parent>' undefined

find_attr_initializers walks cdef.info.mro and reads info.defn.defs.body for AssignmentStmts. ClassDef.serialize (mypy/nodes.py) does not serialize defs, so a cache-loaded parent has defs = Block([]); the MRO walk collects no parent assignments and the subclass's emitted setup leaves inherited slots in the undefined-sentinel state.

This PR implements the fix discussed in the linked issue.

Under `separate=True`, when a subclass is recompiled while its parent
is loaded from mypy's incremental cache, parent default-attribute
assignments are silently dropped from the subclass's
`__mypyc_defaults_setup`. The first read of an inherited default-attr
then raises:

    AttributeError: attribute '<name>' of '<Parent>' undefined

`find_attr_initializers` walks `cdef.info.mro` and reads
`info.defn.defs.body` for `AssignmentStmt`s. `ClassDef.serialize`
(mypy/nodes.py) does not serialize `defs`, so a cache-loaded parent
has `defs = Block([])`; the MRO walk collects no parent assignments
and the subclass's emitted setup leaves inherited slots in the
undefined-sentinel state.

Fix: under `separate=True`, scope `find_attr_initializers` to the
subclass's own body and have `generate_attr_defaults_init` emit a
chained call to the nearest ancestor with `__mypyc_defaults_setup`
before setting own defaults. Each class's setup is responsible only
for its own attributes; the chain runs ancestors first, mirroring
the `__init__` chain. The ancestor's return value is propagated so
a parent default that raised still aborts instance creation. Without
`separate=True`, the MRO AST walk is unaffected (all modules parsed
in the same pass), preserving the existing inline-all behavior.

Adds `testIncrementalCrossModuleInheritedAttrDefaultsWithOverride`
to `run-multimodule.test`, which triggers the cache-load path: clean
build of a two-module Parent/Child hierarchy, then a `.2` revision of
the subclass module to force an incremental rebuild while the parent
is served from the cache. The child overrides one inherited default
so it emits its own `__mypyc_defaults_setup` (the case the chain
composition is required for). The test fails on master without this
patch.
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.

[mypyc] Subclass __mypyc_defaults_setup drops inherited class-attribute initializers across incremental builds

1 participant