-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Cannot define Enum class with a mixin class derived from ABC #119946
Comments
This comment was marked as resolved.
This comment was marked as resolved.
@nineteendo Can you come up with a case where it would break user code? It seems pretty safe to me. If you do not use an ABC-derived mixin with If you do use an ABC-derived mixin, how did you get it to work until now? If you used the If you did some really deep metaclass hacking that modifies the way ABCs work AND you didn't override all of the metaclass methods AND you use your custom ABC machinery together with an If you can construct a plausible concrete case where this might actually break user code, it's likely we can tweak this solution to provide backwards compatibility. |
This comment was marked as resolved.
This comment was marked as resolved.
We have this in EnumMeta = EnumType # keep EnumMeta name for backwards compatibility
class Enum(metaclass=EnumType):
... So in my example, we have that After this change, |
Thanks, I think
|
Agreed. I am guessing that the motivation behind the name change was that it is now also used as a type in typed Python. |
One of the big concerns for enums are their creation times. We'll need performance numbers with and without the ABC addition. |
I think that we should not complicate But, instead you can solve this by adding several lines to your code: >>> import abc
>>> import enum
>>> class A(abc.ABC): ...
...
>>> class MyEnum(A, enum.Enum):
... a = A()
...
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
class MyEnum(A, enum.Enum):
a = A()
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases Solution: >>> class ABCEnum(abc.ABCMeta, enum.EnumType):
... ...
...
>>> class MyEnum(A, enum.Enum, metaclass=ABCEnum):
... a = A()
...
>>> MyEnum.a
<MyEnum.a: <__main__.A object at 0x105210f80>> |
@sobolevn We are not complicating |
Yep, will do. I'm also considering disabling this for construction via the I found some discussion (in which you participated) in #93910. Any other pointers? |
Another point that arises from #93910. In my example in the description, and also in my tests, I implemented the |
@ygale Using |
In addition to enum creation time, Python start-up time is a concern. |
@ethanfurman OK. While I'm at it, I might as well profile member access time as well. |
Bug report
Bug description:
This results in:
There is a work-around by metaclass hacking. But these two basic Python features are expected to Just Work together in a simple, intuitive, and pythonic way.
The fix should be very simple - make
EnumType
a subclass ofABCMeta
. But it's slightly more complicated than that, so that the_simple_enum
decorator and_test_simple_enum
continue to work as expected. See the provided patch.CPython versions tested on:
3.12, 3.13
Operating systems tested on:
macOS
Linked PRs
The text was updated successfully, but these errors were encountered: