Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attribute set_all does not work on complex enums #4939

Open
DanielT opened this issue Feb 26, 2025 · 0 comments
Open

attribute set_all does not work on complex enums #4939

DanielT opened this issue Feb 26, 2025 · 0 comments
Labels

Comments

@DanielT
Copy link

DanielT commented Feb 26, 2025

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

@DanielT DanielT added the bug label Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant