-
Notifications
You must be signed in to change notification settings - Fork 831
Consider support for tuple variants in complex enums #3748
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
Comments
Are there any design blockers here, or is it just a case of someone with sufficient time taking on the implementation? I would assume that it works very similarly to what we've already done for struct variants. Thinking about it, I suspect that the only case which we might want to think about specially is the "newtype" form: #[pyclass]
enum TupleVariants {
String(String),
Int(i64),
} For |
Apart from the obvious things like implementing
Having this in place would allow the patterns in Python For example, given the following Rust enum: #[pyclass]
enum MyEnum {
Tuple(i32, f64, String),
} The following match variant:
case MyEnum.Tuple(a, b, c):
assert isinstance(a, int)
assert isinstance(b, float)
assert isinstance(c, str) Source: https://peps.python.org/pep-0622/#special-attribute-match-args |
#[pyclass]
enum MyEnum {
#[pyo3(match_args = (a, b, c))]
Tuple(i32, f64, String),
}
EDIT: this was incorrect, see below. |
... or maybe I'm misunderstanding, and |
In the pattern
Yep, that's correct!
I agree that I tend to err on the side of minimalism so I think it's better not to allow the user to override the "field names" of a tuple variant. If they want to override the names, they should be using a struct variant instead. :) |
Yep completely agree, ignore my suggestion of |
I can't do: #[pyclass]
#[derive(Debug, FromPyObject)]
pub enum Kind {
Foo {},
Python { inner: PyObject },
}
I did that: #[pyclass]
#[derive(Debug, FromPyObject)]
pub enum FooKind {
Bar { inner: BarKind },
Python { inner: PyObject },
}
#[pyclass]
#[derive(Debug, Clone)]
pub enum BarKind {
FooBar,
} But that not what I wanted and I don't think I can implement |
Why do you need |
Well, I'm still learning pyo3, for now I'm just happy when thing work haha, so there wrapper doesn't do conversion, good to know. |
This got implemented in #4072, closing. |
Allow the following:
pyo3/tests/ui/invalid_pyclass_enum.rs
Lines 24 to 28 in d1b0722
The text was updated successfully, but these errors were encountered: