-
Notifications
You must be signed in to change notification settings - Fork 784
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
Default values in enum struct-variants #4109
Comments
How about allowing #[pyclass]
enum Thing
#[pyo3(signature = (x = None, y = 0, z = 1)]
A {
x: Option<i32>,
y: i32,
z: i32,
}
} This would be comparable to building the class hierarchi manuelly and gives the same control (default, pos/kw only args, ...) as a the constructor for a |
@Icxolu, I think that looks great! Do you plan to open a PR? |
Thanks! Glad you like it. Yes, if people think the syntax and functionality proposed above is workable I'll open a PR from that. Given that |
I really like it too. And actually, I think it could also make sense to extend this feature to regular struct, to auto-generate constructors in a kind of dataclass-like fashion (but not necessarily in the same PR). |
Hello everyone,
Thank you for your great work here. Our project makes extensive use of struct enum variants. However, we have many variants which should have default values: some of those variants are
Option<...>
and should default toNone
if they are not specified. This behavior would be similar to the kwargs in functions, where a default is used if the value is not specified.Currently, for this enum:
I would use it like this:
Thing.A(x=None, y=5, z=1)
.However, setting
x
to None manually makes the code more verbose. It would be easier for the user to do the following:Which would be used like this:
Thing.A(y=5)
wherex=None
andz=1
.I am not sure how difficult this is to implement, but it would be much appreciated to have this feature!
The text was updated successfully, but these errors were encountered: