Skip to content

[mypyc] Fix cross-module class attribute defaults causing KeyError#21012

Open
VaggelisD wants to merge 1 commit intopython:masterfrom
VaggelisD:fix-cross-module-attr-defaults
Open

[mypyc] Fix cross-module class attribute defaults causing KeyError#21012
VaggelisD wants to merge 1 commit intopython:masterfrom
VaggelisD:fix-cross-module-attr-defaults

Conversation

@VaggelisD
Copy link
Contributor

When mypyc compiles a subclass that inherits class attribute defaults from a parent in a different module, the generated __mypyc_defaults_setup method would look that up in the subclass's module globals instead of the parent's, causing KeyError at runtime.

This PR fixes this by tracking the origin module for each default assignment and using a targeted globals_lookup_module override on the builder when evaluating inherited rvalues. This only affects load_globals_dict, not lambda/closure declarations. Also export each module's globals dict in the export table so cross-group access works in separate compilation mode.

When mypyc compiles a subclass that inherits class attribute defaults
from a parent in a different module, the generated __mypyc_defaults_setup
method would look up the parent's module-level variables in the subclass's
module globals instead of the parent's, causing KeyError at runtime.

Fix by tracking the origin module for each default assignment and using
a targeted globals_lookup_module override on the builder when evaluating
inherited rvalues. This only affects load_globals_dict, not lambda/closure
declarations. Also export each module's globals dict in the export table
so cross-group access works in separate compilation mode.
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