Skip to content

bpo-40025:The behavior is the same when _generate_next_value_ is defined afte…#19904

Closed
hongweipeng wants to merge 8 commits into
python:masterfrom
hongweipeng:fix_enum_generate_next_value
Closed

bpo-40025:The behavior is the same when _generate_next_value_ is defined afte…#19904
hongweipeng wants to merge 8 commits into
python:masterfrom
hongweipeng:fix_enum_generate_next_value

Conversation

@hongweipeng

@hongweipeng hongweipeng commented May 4, 2020

Copy link
Copy Markdown
Contributor

…r or before members

https://bugs.python.org/issue40025

@ethanfurman ethanfurman left a comment

Copy link
Copy Markdown
Member

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.

Comment thread Lib/enum.py Outdated
Comment thread Lib/enum.py Outdated
Comment thread Lib/enum.py Outdated
@bedevere-bot

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants