From 845be0486adb5b1679a6644a76c1a0f01b6ee1b0 Mon Sep 17 00:00:00 2001 From: Gabriel Smith Date: Thu, 16 Jun 2022 15:51:32 -0400 Subject: [PATCH] guide: Update with enum naming macro changes --- guide/src/class.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/guide/src/class.md b/guide/src/class.md index 7124114a..b87d8b0b 100644 --- a/guide/src/class.md +++ b/guide/src/class.md @@ -897,6 +897,26 @@ Python::with_gil(|py| { }) ``` +Enums and their variants can also be renamed using `#[pyo3(name)]`. + +```rust +# use pyo3::prelude::*; +#[pyclass(name = "RenamedEnum")] +enum MyEnum { + #[pyo3(name = "UPPERCASE")] + Variant, +} + +Python::with_gil(|py| { + let x = Py::new(py, MyEnum::Variant).unwrap(); + let cls = py.get_type::(); + pyo3::py_run!(py, x cls, r#" + assert repr(x) == 'RenamedEnum.UPPERCASE' + assert x == cls.UPPERCASE + "#) +}) +``` + You may not use enums as a base class or let enums inherit from other classes. ```rust,compile_fail