-
Notifications
You must be signed in to change notification settings - Fork 805
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
Comparing enums by identity #3059
Comments
Hello, I agree with you that this is important; I've been aware of this shortcoming in PyO3's enum implementation and while I've wanted to fix it for a while I haven't had any time available to invest in this particular issue. Your offer to help implement is appreciated, I'd be happy to feedback on the design choices with you and help steer an implementation. To point you in the right direction, there's a couple of inter-related pieces which would be worth reading and thinking about:
|
I believe this is the way to go, otherwise it's required to use the class attributes everywhere where the enum is created. See the test I've added in 82353cc. Now it's possible to use |
e._reason is an enum from cryptography.exceptions._Reasons so "is" should be the correct comparison, but it doesn't always work. This is apparently triggered by _Reasons moving to the part of cryptography that is implemented in rust, which doesn't yet implement enums as singletons. pyca/cryptography#11332 PyO3/pyo3#3059
e._reason is an enum from cryptography.exceptions._Reasons so "is" should be the correct comparison, but it doesn't always work. This is apparently triggered by _Reasons moving to the part of cryptography that is implemented in rust, which doesn't yet implement enums as singletons. pyca/cryptography#11332 PyO3/pyo3#3059
In python the convention is to compare enums by identity (
Enum.A is Enum.A
andEnum.A is not Enum.B
) instead of equality (Enum.A == Enum.A
andEnum.A != Enum.B
). However, a straight forward implementation of enums in rust exposed to python using pyo3 does not support this. The documentation also only mentions comparison by equality.For the particular use-case I'm working with, it is used like the following:
In #2384 there is a description on how to use
GILOnceCell
and#[classattr]
to return pointers to the same instance of an enum such that one can compare the enums by identity. Ideally there would be a way to achieve this result without that amount of boilerplate code per enum variant. Would it be possible to automatically generate this means of some setting in#[pyclass]
?I would be able to implement this if there is some guidance and a clear idea on how to support it.
The text was updated successfully, but these errors were encountered: