Skip to content

[mypyc] Recompile dependents when a class's native_class or @trait changes - #22136

Merged
JukkaL merged 1 commit into
python:masterfrom
rheard:fix-mypyc-1232
Oct 9, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
rheard:fix-mypyc-1232

Conversation

@rheard

@rheard rheard commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

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 imported mypyc_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'). @trait had 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_data now 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 same is_extension_class, is_trait and is_decorated_class calls as build_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.py and checks that other, 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_attr flags that affect subclasses in other modules, such as serializable, have the same gap. I left them out since they'd mean repeating how prepare_class_def derives them, but I can add them here if you'd like.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@JukkaL
JukkaL merged commit 28ed22b into python:master Oct 9, 2026
18 checks passed
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.
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.

Separate compilation doesn't recompile modules that use a class when @mypyc_attr(native_class=False) or @trait is added

2 participants