-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
enum: _generate_next_value_ is not called if its definition occurs after calls to auto() #84206
Comments
I ran into this issue when attempting to add a custom _generate_next_value_ method to an existing Enum. Adding the method definition to the bottom of the class causes it to not be called at all: from enum import Enum, auto
class E(Enum):
A = auto()
B = auto()
def _generate_next_value_(name, *args):
return name E.B.value # Returns 2, E._generate_next_value_ is not called class F(Enum):
def _generate_next_value_(name, *args):
return name
A = auto()
B = auto()
F.B.value # Returns 'B', as intended I do not believe that the order of method/attribute definition should affect the behavior of the class, or at least it should be mentioned in the documentation. |
Immediate solution is to raise an exception if Possible future solution is to save all member definitions until after class is defined. The exception-raising solution would require a check in |
While the current behavior may be initially unexpected, it does match the way that python normally behaves when defining class variables. For example, the following class will throw an exception because the function number_two() is called before it is defined: class Numbers:
one = 1
two = number_two()
def number_two():
return 2 # NameError: name 'number_two' is not defined However, this version is fine: class Numbers:
one = 1
def number_two():
return 2
two = number_two() |
Jonathan Hsu, you are correct -- and "don't do that" was my initial response; but Enum takes many pains to make sure the user doesn't shoot themselves in the foot, so raising a TypeError is appropriate instead of silently doing the wrong thing. |
Thank you for the explanation. |
Hi, I have ran the code with with _generate_next_value_ method at the bottom of the class and didn't run into any exceptions. Please refer my python file. Please let me know if I am missing something. from enum import Enum, auto
class E(Enum):
A = auto()
B = auto()
def _generate_next_value_(name, *args):
return name
for l in (E):
print(l.name)
print(l.value) |
Ankesh, that is the expected behavior as no patch has been merged yet. |
We can close this? |
Not yet. I want to investigate the idea Ankesh Saha had some more. |
There was an effort to make it so class I(Enum):
first = auto()
second = first + 2 # this line would fail Closing the ticket. Thank you everyone! |
_generate_next_value_
is defined afte… #19904Note: 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: