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

enums generated by Pyo3 don't actually subclass Python's Enum #2887

Open
yodaldevoid opened this issue Jan 18, 2023 · 2 comments
Open

enums generated by Pyo3 don't actually subclass Python's Enum #2887

yodaldevoid opened this issue Jan 18, 2023 · 2 comments

Comments

@yodaldevoid
Copy link
Contributor

enums generated by Pyo3 don't actually subclass Python's Enum. Due to this, some attributes/methods are missing from Pyo3 enums that you would expect in Python. Additionally, EnumType is not the metaclass of Pyo3 enums leading to the same problems.

I am not entirely sure how this might be fixed, simply because I don't know the restrictions of the Python C API.

@yodaldevoid yodaldevoid changed the title enums generated by Pyo3 don't actually subclass Python's Enum. enums generated by Pyo3 don't actually subclass Python's Enum Jan 18, 2023
@davidhewitt
Copy link
Member

Enum isn't exposed in the C-API, so if we were to add the inheritance we would first need to solve #991.

More likely we could add methods to our exposed enums to match what the expected interface would be. This would be a case of someone enumerating what's missing and we decide whether to support it or not.

@yodaldevoid
Copy link
Contributor Author

That is what I assumed was the answer. For future reference, this is the total list of attributes missing from enums as they stand, to the best of my understanding.

From EnumType and Enum:

  • __contains__
  • __getitem__
  • __iter__
  • __len__
  • __reversed__
  • __call__
  • __members__
  • _name_
  • name
  • _value_
  • value
  • _missing_

As a side note, __new__ is supposed to return a list of the Enum members, which I'm not sure it does right now.

Enums are supposed to be able to be accessed via either their value or their name. e.g. ExampleEnum(0) or ExampleEnum['example_member'] The value access is handled __call__ and the name access by __getitem__.

All Enum members need a respective value, whether unique or not. Values do not need to be numbers.

There are additional attributes expected on some standard Enum subclasses, but leaving those to the user or a later date seems fine.

https://docs.python.org/3/library/enum.html
https://peps.python.org/pep-0435/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants