[mypyc] Preserve inherited attribute defaults under separate=True#21547
Open
georgesittas wants to merge 1 commit into
Open
[mypyc] Preserve inherited attribute defaults under separate=True#21547georgesittas wants to merge 1 commit into
georgesittas wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:find_attr_initializerswalkscdef.info.mroand readsinfo.defn.defs.bodyforAssignmentStmts.ClassDef.serialize(mypy/nodes.py) does not serializedefs, so a cache-loaded parent hasdefs = 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.