[mypyc] Report an error if an acyclic class inherits from non-acyclic#21227
Merged
p-sawicki merged 3 commits intopython:masterfrom Apr 15, 2026
Merged
Conversation
JukkaL
approved these changes
Apr 14, 2026
| pass | ||
|
|
||
| @mypyc_attr(acyclic=True) | ||
| class BadGeneric(Generic[T]): # E: "acyclic" can't be used in a class that inherits from non-acyclic type "typing.Generic" |
Collaborator
There was a problem hiding this comment.
What about testing traits and non-native classes as bases (fine to do this in a follow-up PR)?
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.
Classes marked with
@mypyc_attr(acyclic=True)are generated without thePy_TPFLAGS_HAVE_GCflag which opts them out of the cyclic GC. However, when they inherit from a class that does have this flag, cpython opts them back into the cyclic GC ininherit_specialhttps://github.com/python/cpython/blob/main/Objects/typeobject.c#L8614.The destructor generated by mypyc for acyclic classes does not untrack the instances before deallocating them which makes cpython issue a warning in debug builds https://github.com/python/cpython/blob/main/Python/gc.c#L2538. I wasn't able to confirm whether this can cause memory leaks but it's probably best to disallow this case.
This also affects generic classes that use the new type parameter syntax
class C[T]. They implicitly inherit fromtyping.Genericwhich opts into GC https://github.com/python/cpython/blob/main/Objects/typevarobject.c#L2350.