You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cross-module enum class imports fail when classes are split across generated modules #236
When using cdef enum class with wrap-attach: directive, autowrap splits the generated code across multiple _pyopenms_N.pyx files for parallel compilation. However, when an enum class is defined in one module (e.g., _pyopenms_2.pyx) but used as a parameter type in a class method that ends up in a different module (e.g., _pyopenms_5.pyx), the necessary cross-module import is missing.
#131 - [TEST] example for enums in namespaces [closed] #212 - Fix handling of wrap-ignore for foreign cimports (#196) [merged] #221 - Add test for scoped enum forward declaration bug [merged] #222 - Use IntEnum instead of Enum for enum class wrappers [merged] #234 - Add test for enum-based overload resolution [merged]
🧪 Issue enrichment is currently in open beta.
You can configure auto-planning by selecting labels in the issue_enrichment configuration.
To disable automatic issue enrichment, add the following to your .coderabbit.yaml:
issue_enrichment:
auto_enrich:
enabled: false
💬 Have feedback or questions? Drop into our discord!
ifresolved.cpp_decl.annotations.get("wrap-attach"):
# No need to import attached classes as they are# usually in the same pxd file and should not be# globally exported.pass# <-- BUG: Skips the import entirely
The False Assumption
The comment assumes attached enums are "usually in the same pxd file" and therefore don't need cross-module imports. However, autowrap's module splitting (via chunkIt in pyOpenMS's create_cpp_extension.py) distributes pxd files across output modules alphabetically, so:
But no cross-module Python imports are generated for these.
Proposed Fix
Add a new method to generate Python imports for enum classes:
defcreate_foreign_enum_imports(self):
"""Generate Python imports for enum classes used in type assertions across modules"""code=Code()
formoduleinself.all_decl:
mname="."+moduleifsys.version_info>= (3, 0) andself.add_relativeelsemoduleifos.path.basename(self.target_path).split(".pyx")[0] !=module:
forresolvedinself.all_decl[module]["decls"]:
ifresolved.__class__in (ResolvedEnum,):
ifnotresolved.wrap_ignore:
# Generate Python import for the _Py prefixed enum classpy_name="_Py"+resolved.namecode.add("from $mname import $py_name", locals())
self.top_level_code.append(code)
Call this in create_pyx_file() after create_foreign_cimports():
self.create_foreign_cimports()
self.create_foreign_enum_imports() # Add this line
Note: This is NOT Cython Issue #5609
The Cython issue #5609 is about import/cimport order sensitivity within a single module. Our issue is about missing imports entirely across modules - a different problem with a different fix location (autowrap, not Cython).
I guess it makes sense. But I have to say I am not too familiar with the multi module mechanisms.
E.g. when something is compiled and imported as a separate module and when something is just imported from a pxd in the same module. Then of course there is difference in python imports and cimports.
Activity
coderabbitai commentedon Jan 7, 2026
📝 CodeRabbit Plan Mode
Generate an implementation plan and prompts that you can use with your favorite coding agent.
Examples
🔗 Related PRs
#131 - [TEST] example for enums in namespaces [closed]
#212 - Fix handling of wrap-ignore for foreign cimports (#196) [merged]
#221 - Add test for scoped enum forward declaration bug [merged]
#222 - Use IntEnum instead of Enum for enum class wrappers [merged]
#234 - Add test for enum-based overload resolution [merged]
🧪 Issue enrichment is currently in open beta.
You can configure auto-planning by selecting labels in the issue_enrichment configuration.
To disable automatic issue enrichment, add the following to your
.coderabbit.yaml:💬 Have feedback or questions? Drop into our discord!
timosachsenberg commentedon Jan 7, 2026
Root Cause Analysis
After deep investigation, I've identified the exact location of the bug and a proposed fix.
Bug Location
File:
autowrap/CodeGenerator.py, methodcreate_foreign_cimports()(lines ~2008-2013)The False Assumption
The comment assumes attached enums are "usually in the same pxd file" and therefore don't need cross-module imports. However, autowrap's module splitting (via
chunkItin pyOpenMS'screate_cpp_extension.py) distributes pxd files across output modules alphabetically, so:IonSource.pxd(definesPolarityenum) →_pyopenms_2.pyxIonSourceclass methods usingPolarity→_pyopenms_5.pyxTwo Missing Import Layers
cimport)from .module cimport EnumTypewrap-attachenumsimport)from .module import _PyEnumNameThe Python-level
_PyEnumNameclasses (e.g.,_PyPolarity,_PySpectrumType) are used in type assertions:But no cross-module Python imports are generated for these.
Proposed Fix
Add a new method to generate Python imports for enum classes:
Call this in
create_pyx_file()aftercreate_foreign_cimports():Note: This is NOT Cython Issue #5609
The Cython issue #5609 is about import/cimport order sensitivity within a single module. Our issue is about missing imports entirely across modules - a different problem with a different fix location (autowrap, not Cython).
Fix cross-module enum class imports for type assertions
Fix enum class support: cross-module imports and return type conversion
jpfeuffer commentedon Jan 7, 2026
I guess it makes sense. But I have to say I am not too familiar with the multi module mechanisms.
E.g. when something is compiled and imported as a separate module and when something is just imported from a pxd in the same module. Then of course there is difference in python imports and cimports.
Confusing.
Fix cross-module enum class imports for type assertions (OpenMS#237)