Skip to content

Commit

Permalink
guide: Update with enum naming macro changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yodaldevoid committed Jun 21, 2022
1 parent 0f74cd4 commit ccb63b9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MyEnum>();
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
Expand Down

0 comments on commit ccb63b9

Please sign in to comment.