Repository navigation
[mypyc] Fix undeclared module in C when base class is a re-export - #22357
Merged
Merged
Conversation
Fixes mypyc/mypyc#1163. Fixes mypyc/mypyc#1155. Fixes mypyc/mypyc#1033. If a non-native class (for example, an enum) had a base class that was imported through a re-export, the generated C failed to compile: ```python from backports.strenum import StrEnum # Defined in backports.strenum.strenum class C(StrEnum): # error: 'CPyModule_backports___strenum___strenum' undeclared x = "x" ``` The base class was loaded from the module where it's defined, but that module wasn't explicitly imported. Subclassing `ctypes.Structure` (defined in `_ctypes`) had the same issue. Now base classes from other modules are loaded using `get_module_attr`, which imports the module if needed. This also fixes two related issues with non-native classes. If the defining module was imported only in a function body that hadn't run yet, the base was looked up from None. A nested class used as a base (such as `Outer.Inner`) generated an undeclared module reference for `Outer`, since the module name was derived from the fullname.
p-sawicki
approved these changes
Nov 3, 2026
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#1163. Fixes mypyc/mypyc#1155. Fixes mypyc/mypyc#1033.
If a non-native class (for example, an enum) had a base class that was imported through a re-export, the generated C failed to compile:
The base class was loaded from the module where it's defined, but that module wasn't explicitly imported. Subclassing
ctypes.Structure(defined in_ctypes) had the same issue. Now base classes from other modules are loaded usingget_module_attr, which imports the module if needed.This also fixes two related issues with non-native classes. If the defining module was imported only in a function body that hadn't run yet, the base was looked up from None. A nested class used as a base (such as
Outer.Inner) generated an undeclared module reference forOuter, since the module name was derived from the fullname.