pyo3
pyo3 copied to clipboard
attribute set_all does not work on complex enums
Bug Description
I can annotate a complex enum as #[pyclass(get_all, set_all)], but set_all does not work. It should either work or be rejected during compilation.
Steps to Reproduce
To demonstrate the issue I wrote the following code:
use pyo3::prelude::*;
#[pyclass(get_all, set_all)]
#[derive(Debug, Clone)]
struct ItemValues {
number: u8,
}
#[pymethods]
impl ItemValues {
#[new]
fn new(number: u8) -> Self {
ItemValues { number }
}
fn __repr__(&self) -> String {
format!("{:#?}", self)
}
}
#[pyclass(get_all, set_all)]
#[derive(Debug, Clone)]
enum ComplexEnum {
Item(ItemValues),
OtherItem {
value: usize,
},
}
#[pymethods]
impl ComplexEnum {
fn __repr__(&self) -> String {
format!("{:#?}", self)
}
}
#[pymodule]
fn pyo3_test(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<ItemValues>()?;
m.add_class::<ComplexEnum>()?;
Ok(())
}
After building and installing the module, I tested it in the REPL:
>>> from pyo3_test import *
>>> enumval = ComplexEnum.Item(ItemValue(55))
>>> enumval
Item(
ItemValues {
number: 55,
},
)
>>> enumval[0].number = 1
>>> enumval
Item(
ItemValues {
number: 55,
},
)
In this first test we see that writing to the number did not raise an error, but had no effect.
Next I try to set the value of OtherItem:
>>> other = ComplexEnum.OtherItem(3)
>>> other
OtherItem {
value: 3,
}
>>> other.value = 4
Traceback (most recent call last):
File "<python-input-9>", line 1, in <module>
other.value = 4
^^^^^^^^^^^
AttributeError: attribute 'value' of 'builtins.ComplexEnum_OtherItem' objects is not writable
This at least raises an error, which is better than discarding thewrite silently, but it is still unexpected considering the set_all attribute was accepted.
Backtrace
Your operating system and version
Windows 10
Your Python version (python --version)
Python 3.13.2
Your Rust version (rustc --version)
rustc 1.85.0
Your PyO3 version
0.23.4
How did you install python? Did you use a virtualenv?
Yes, I'm using a virtualenv
Additional Info
No response