Commit d1d220f
committed
Add native_enum: expose Rust enums as Python enum.Enum subclasses
Introduce `#[py_native_enum]` / `#[derive(NativeEnum)]` macros that expose
a fieldless Rust enum to Python as a true `enum.Enum` subclass via the
functional API, without touching the C-API enum machinery (#991).
Runtime (`src/native_enum/`):
- `spec.rs` – `NativeEnumSpec`, `NativeEnumBase`, `VariantValue`
- `base_cache.rs` – per-interpreter `PyOnceLock` cache for base classes
(`enum.Enum`, `IntEnum`, `StrEnum`, `Flag`, `IntFlag`) and `enum.auto`
- `construct.rs` – `build_native_enum`: builds a Python enum subclass
from a spec without caching
- `trait_def.rs` – `NativeEnum` trait with `py_enum_class`,
`to_py_member`, `from_py_member`
Macro (`pyo3-macros-backend/src/native_enum.rs`):
- Parses enum-level attrs (`base`, `rename`, `module`) and variant-level
attrs (`rename`, `value`); supports integer discriminants
- Caches the Python class in a function-local `static PyOnceLock<Py<PyType>>`
in the generated `py_enum_class`, constructing it once per interpreter session
- `from_py_member` checks `isinstance(obj, cached_class)` for type safety
- Auto-derives `IntoPyObject`, `IntoPyObject for &T`, `FromPyObject`
Public API additions to `pyo3-macros/src/lib.rs`:
- `#[proc_macro_derive(NativeEnum, attributes(native_enum))]`
- `#[proc_macro_attribute] py_native_enum`1 parent f57bda7 commit d1d220f
12 files changed
Lines changed: 852 additions & 4 deletions
File tree
- pyo3-benches
- benches
- pyo3-macros-backend/src
- pyo3-macros/src
- src
- native_enum
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
89 | 93 | | |
90 | 94 | | |
91 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| |||
0 commit comments