Skip to content

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

Merged
p-sawicki merged 7 commits into
python:masterfrom
VaggelisD:fix/mypyc-defaults-setup-cache-load
Jun 3, 2026
Merged

[mypyc] Preserve inherited attribute defaults under separate=True#21547
p-sawicki merged 7 commits 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.
Comment thread mypyc/irbuild/classdef.py Outdated
Comment thread mypyc/irbuild/classdef.py Outdated
Comment thread mypyc/irbuild/classdef.py Outdated
Comment thread mypyc/irbuild/prepare.py Outdated
Comment thread mypyc/irbuild/util.py Outdated
@p-sawicki p-sawicki merged commit 52de0c7 into python:master Jun 3, 2026
18 checks passed
@georgesittas georgesittas deleted the fix/mypyc-defaults-setup-cache-load branch June 4, 2026 09:38
georgesittas added a commit to VaggelisD/sqlglot-mypy that referenced this pull request Jun 4, 2026
…thon#21547)

Fixes python#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 `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.

This PR implements the fix discussed in the linked issue.
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

2 participants