Skip to content

[mypyc] Fix undeclared module in C when base class is a re-export - #22357

Merged
JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1163-reexported-base
Nov 3, 2026
Merged

JukkaL merged 1 commit into
masterfrom
mypyc-bulkfix-1163-reexported-base

Conversation

@JukkaL

@JukkaL JukkaL commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

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:

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.

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.
@JukkaL
JukkaL merged commit 623bef9 into master Nov 3, 2026
18 checks passed
@JukkaL
JukkaL deleted the mypyc-bulkfix-1163-reexported-base branch November 3, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants