Repository navigation
[mypyc] Recompile dependents when a class's native_class or @trait changes - #22136
Merged
Merged
Conversation
p-sawicki
pushed a commit
that referenced
this pull request
Nov 24, 2026
…cyclic is toggled on a base class (#22467) `@mypyc_attr(weakrefs=True)` (added in #22387) adds a weak reference list field to the object struct of the class, right after the vtable pointer, so the attributes of every native subclass move and `tp_basicsize` grows. With separate compilation, toggling it on a base class didn't regenerate subclasses in other groups. Mypy doesn't record class decorators, so the base module's interface hash didn't change, and the subclass module kept its old C code with the old layout. Example, compiled with `mypycify(["base_mod.py", "sub_mod.py"], separate=True)`: ```python # base_mod.py from mypy_extensions import mypyc_attr class A: # @mypyc_attr(weakrefs=True) added before the second build def __init__(self) -> None: self.x = 111 self.s = "hello" # sub_mod.py from base_mod import A class B(A): def __init__(self) -> None: super().__init__() self.y = 222 self.t = "world" ``` After the second build `B.__basicsize__` was 56 instead of 64, and accessing attributes of a `B` instance and creating a weak reference to it segfaulted. Expected: `sub_mod` is recompiled and everything works. The kinds of a module's classes are already part of its interface through `MypycPlugin.report_config_data`, for `native_class=False` and `@trait` (#22136). This adds the `weakrefs` flag to the tuple `class_kinds()` returns. When the cache is written it's derived from the tree the same way `prepare_class_def` sets `ClassIR._weakrefs` (only for native classes that aren't traits), and when the cache is checked it comes from `_weakrefs` in the cached IR, so the two agree when nothing changed. I added `testIncrementalCompilationTogglingWeakrefs` to run-multimodule.test, following the existing tests for `native_class` and `@trait`: it turns `weakrefs` on and off on a base class in `other.py`, and in the middle step it changes only `native.py` to check that `other` stays fresh. Without the fix, the separate compilation variant crashes in step 2. The same gap existed for two more `@mypyc_attr` flags, fixed in a second commit. `serializable` is inherited, and a serializable class's `tp_new` doesn't call `__init__`. After making a base class serializable, a stale subclass in another group still called `__init__` without arguments, so `copy.copy()`/`pickle` of the subclass failed with a `TypeError` about missing arguments. `acyclic` is only allowed when all base classes are acyclic. After making a base class non-acyclic, the error for an acyclic subclass wasn't reported. CPython then made the subclass a GC type with the base's `tp_traverse`, but its `tp_dealloc` doesn't untrack instances, which can segfault if a collection runs while an instance is freed. Both flags are now part of the class kinds (`_serializable` and `is_acyclic` on the cached-IR path), with a new run-multimodule test for each. `allow_interpreted_subclasses` doesn't need this: a subclass's generated code doesn't depend on its base having the flag. The only stale case is a base losing it while a subclass still uses it, and then the build fails with a C compile error instead of mypyc's error message.
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 mypyc/mypyc#1232.
With separate compilation, adding
@mypyc_attr(native_class=False)to a class between builds, or removing it, didn't recompile the modules that use the class if its module already importedmypyc_attr. Mypy doesn't record the decorator, so the module's interface hash didn't change. The stale modules then failed to compile against the class's new export table ('CPyDef_a___C': is not a member of 'export_table_a').@traithad the same problem, and there the result was a segfault: the trait's module looked for a trait vtable that the stale subclasses didn't have.This is the follow-up mentioned in #22128, using the same approach.
MypycPlugin.report_config_datanow returns(name, is_ext_class, is_trait, is_decorated)for each class in the module, in place of the list of decorated class names that #22128 added, and mypy includes the plugin config data in the interface hash. When the cache is written, these come from the tree, using the sameis_extension_class,is_traitandis_decorated_classcalls asbuild_type_map. When the cache is checked, the module hasn't been parsed yet, so they come from the cached IR.The new tests turn each decorator on and off. A step in the middle changes only
native.pyand checks thatother, which then has a non-native class or a trait, stays fresh, so a mismatch between the write and check paths would show up.Other
mypyc_attrflags that affect subclasses in other modules, such asserializable, have the same gap. I left them out since they'd mean repeating howprepare_class_defderives them, but I can add them here if you'd like.