Subscriptable enums #3395
Unanswered
markkimsal
asked this question in
Questions
Replies: 2 comments 1 reply
-
I think I figured it out with __class_getitem__ #[staticmethod]
fn __class_getitem__(item: &str) -> PyResult<Self> {
match item {
"INVESTMENT" => Ok(Self::INVESTMENT),
"RETIREMENT" => Ok(Self::RETIREMENT),
"IRA" => Ok(Self::IRA),
_ => Err(PyKeyError::new_err("key not found in enum")),
}
} Although, this function is not mentioned in #2887 (comment) |
Beta Was this translation helpful? Give feedback.
0 replies
-
I guess the __class_getitem__ solution is for the current class based, not-real-enum solution and the previously mentioned issue is about implementing more "real" enums. Is that a correct assumption? It would be nice to see some validation that this is the right way to go given the current # [pyclass] implementation |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have some Python code that uses enums with dynamic subscript string keys. I can't seem to figure out how to define then in Rust and export them to Py. When I try to define
__getitem__
it doesn't seem to work because it's passing a Type or Class and not an instance of the object (because it's an enum, not an object).When I try to use it form python, it says
TypeError: 'type' object is not subscriptable
If I try to call getItem directly it says that
___getitem___ requires a 'builtins.AccountType' object, but received a 'str'
I've tried adding # [staticmethod] to getitem as well.
Anyone have an idea how to make this somewhat doable? Like using a dictionary instead of an enum maybe? Or write matching parsing/matching functions in both languages?
Beta Was this translation helpful? Give feedback.
All reactions