Skip to content

Conversation

@hongweipeng
Copy link
Contributor

@hongweipeng hongweipeng commented May 4, 2020

…r or before members

https://bugs.python.org/issue40025

Copy link
Member

@ethanfurman ethanfurman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, but it needs more work. Currently, even if _generate_next_value_ is defined before any members it still will not be called until after the entire Enum is defined and given to EnumMeta.__new__ for final processing, which means that somebody wanting to do interesting things with previous member values would not be able to.

In _EnumDict you'll need to track if auto() has been seen yet, if _generate_next_value_ was defined before any uses of auto() were, and call _generate_next_value_ within _EnumDict if and only if _generate_next_value_ was defined before all uses of auto().

So this should work:

class Test1(Enum): def _generate_next_value(...): return 12 first = auto() second = first + 9 

and this should fail:

class Test2(Enum): first = auto() def _generate_next_value(...): return 12 second = first + 9 

Also, make sure you have spaces after commas, etc.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@hongweipeng

This comment has been minimized.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@ethanfurman: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from ethanfurman May 5, 2020 06:01
@ethanfurman
Copy link
Member

I had hoped to incorporate this idea when I had time to look at it further; unfortunately, now that I have I do not see a way to support the following use-case (which I believe would be much more common):

class I(Enum): first = auto() second = first + 2 

HongWeipeng, thank you for trying.

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