Expand description
§pyenum
Expose Rust enum types to Python as genuine enum.Enum subclasses via
PyO3 — #[derive(PyEnum)], functional-API class construction, a
per-interpreter cache, and IntoPyObject / FromPyObject conversion
plumbing generated by the derive.
§Scope
- Unit-variant enums only; tuple / struct / generic / lifetime-parameterised enums are rejected at compile time.
- Five Python enum bases are selectable:
Enum(default),IntEnum,StrEnum,Flag,IntFlag. - Rust variant names are passed through unchanged (Python member name == Rust identifier).
- The Python class is constructed exactly once per interpreter and cached
via
pyo3::sync::PyOnceLock. - PyO3 0.28, CPython 3.11+.
- Free-threaded (
--disable-gil) Python, module reload, and sub-interpreter finalisation are out of scope.
Structs§
- PyEnum
Spec - Static metadata emitted by
#[derive(PyEnum)]for each derived enum.
Enums§
- PyEnum
Base - Python enum base type selector.
- Variant
Literal - A single variant’s declared value, ready to be materialised into a
Python-side
(name, value)tuple for the functionalenum.*constructor.
Traits§
- PyEnum
Trait - Bridge between a
#[derive(PyEnum)]Rust type and its cached Python class. - PyModule
Ext - Extension-method form of
add_enum—m.add_enum::<T>()?inside a#[pymodule]is the idiomatic call site.
Functions§
- add_
enum - Register the Python class for
TontomoduleunderT::SPEC.name.
Derive Macros§
- PyEnum
- Derive a
pyenum::PyEnumimplementation for a unit-variant Rust enum.