Skip to content

Commit cf595fb

Browse files
Use IntEnum instead of Enum for enum class wrappers (#222)
Change the generated Python enum wrappers to inherit from IntEnum instead of Enum. This fixes a bug where set parameters containing enum class values would fail with "TypeError: an integer is required" when iterating. The issue was that: 1. Generated code declares `cdef int item0` for iteration 2. But iterating over a set of Enum values yields Enum objects, not ints 3. Cython cannot assign an Enum object to `cdef int` With IntEnum: - Enum values ARE integers (isinstance(val, int) is True) - Cython's `cdef int` can directly accept IntEnum values - Round-trip operations (get → set) work correctly - Backwards compatible with code expecting integers Tested with pyOpenMS which uses `cdef enum class ActivationMethod`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0501161 commit cf595fb

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

autowrap/CodeGenerator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def create_for(
348348

349349
pyi_code = "from __future__ import annotations\n"
350350
pyi_code += "from typing import overload, Any, List, Dict, Tuple, Set, Sequence, Union\n\n"
351-
pyi_code += "from enum import Enum as _PyEnum\n\n"
351+
pyi_code += "from enum import IntEnum as _PyEnum\n\n"
352352
pyi_code += "\n".join(ci.render() for ci in self.top_level_typestub_code)
353353
pyi_code += "\n\n"
354354
for n, c in self.typestub_codes.items():
@@ -2040,7 +2040,7 @@ def create_default_cimports(self):
20402040
|#Generated with autowrap %s and Cython (Parser) %s
20412041
|#cython: c_string_encoding=ascii
20422042
|#cython: embedsignature=False
2043-
|from enum import Enum as _PyEnum
2043+
|from enum import IntEnum as _PyEnum
20442044
|from cpython cimport Py_buffer
20452045
|from cpython cimport bool as pybool_t
20462046
|from libcpp.string cimport string as libcpp_string

0 commit comments

Comments
 (0)