-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
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
Able to subclass enum with members by using multiple inheritance #85689
Comments
when inheriting an implemented enum, we get a runtime error ------------------------------------ from enum import IntEnum, Enum
class A(IntEnum):
a = 1
class B(A, Enum):
b= 1
print(B.b) |
Could you provide code which demonstrates the problem? |
Eric V. Smith it's in the first comment |
With Python 3.7.4, which is all I have handy, that code does not give a runtime error. It prints "B.b". Your text says code "when creating a multiple inheritance like the following, it works". It sounds like you know the code sample works. So I don't understand what code gives a runtime error. What type of error are you seeing? |
sorry, I'll explain. by "when inheriting an implemented enum, we get a runtime error" I refer to this https://docs.python.org/3/library/enum.html#restricted-enum-subclassing You shouldn't be able to subclass an enum, yet I just showed a loophole. my example code shows you can subclass an enum by using a trick, multiple inheritance. I hope it's more clear now :) btw it also works with regular Enum inheritance (I just used IntEnum for simplicity) |
The problem is that class B should raise an error as class A already has members. |
Ethan Furman bullseye! thanks for clarifying |
The documentation says: "Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances." but it isn't clear what those invariants are, or why it is more of a problem for enums than any other subclassing situation. Could the docs be updated to explain why it is prohibited? |
To answer the invariant question, see this post by Guido: https://mail.python.org/pipermail/python-dev/2013-April/125716.html |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: