
This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.
Created on2020-03-20 11:25 byedd07, last changed2022-04-11 14:59 byadmin. This issue is nowclosed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| Issue40025.PNG | ankeshsaha,2020-03-30 15:23 | Screenshot of the code and output | ||
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 19098 | merged | Ethan Onstott,2020-03-21 05:15 | |
| PR 19762 | merged | miss-islington,2020-04-28 17:22 | |
| PR 19763 | merged | miss-islington,2020-04-28 17:22 | |
| PR 19904 | closed | hongweipeng,2020-05-04 16:37 | |
| PR 22284 | merged | ethan.furman,2020-09-16 18:20 | |
| PR 22285 | merged | ethan.furman,2020-09-16 18:33 | |
| PR 22287 | merged | miss-islington,2020-09-17 00:26 | |
| PR 22286 | merged | miss-islington,2020-09-17 00:26 | |
| Messages (13) | |||
|---|---|---|---|
| msg364665 -(view) | Author: Luis E. (edd07) | Date: 2020-03-20 11:25 | |
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, autoclass E(Enum):A = auto()B = auto()def _generate_next_value_(name, *args):return nameE.B.value # Returns 2, E._generate_next_value_ is not calledclass F(Enum):def _generate_next_value_(name, *args):return nameA = auto()B = auto()F.B.value # Returns 'B', as intendedI 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. | |||
| msg364707 -(view) | Author: Ethan Furman (ethan.furman)*![]() | Date: 2020-03-20 19:09 | |
Immediate solution is to raise an exception if `_generate_next_value_` is defined after members.Possible future solution is to save all member definitions until after class is defined.The exception-raising solution would require a check in `_EnumDict` where `_generate_next_value_` is saved -- if any members already exist, raise. | |||
| msg364776 -(view) | Author: Jonathan Hsu (Jonathan Hsu)* | Date: 2020-03-21 21:45 | |
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 definedHowever, this version is fine:class Numbers: one = 1 def number_two(): return 2 two = number_two() | |||
| msg364784 -(view) | Author: Ethan Furman (ethan.furman)*![]() | Date: 2020-03-22 05:20 | |
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. | |||
| msg364967 -(view) | Author: Jonathan Hsu (Jonathan Hsu)* | Date: 2020-03-25 00:55 | |
Thank you for the explanation. | |||
| msg365321 -(view) | Author: Ankesh Saha (ankeshsaha)* | Date: 2020-03-30 15:23 | |
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, autoclass E(Enum):A = auto()B = auto()def _generate_next_value_(name, *args):return namefor l in (E):print(l.name)print(l.value) | |||
| msg367538 -(view) | Author: Ethan Onstott (Ethan Onstott)* | Date: 2020-04-28 15:51 | |
Ankesh, that is the expected behavior as no patch has been merged yet. | |||
| msg367547 -(view) | Author: Ethan Furman (ethan.furman)*![]() | Date: 2020-04-28 17:21 | |
New changesetd9a43e20facdf4ad10186f820601c6580e1baa80 by Ethan Onstott in branch 'master':bpo-40025: Require _generate_next_value_ to be defined before members (GH-19098)https://github.com/python/cpython/commit/d9a43e20facdf4ad10186f820601c6580e1baa80 | |||
| msg370127 -(view) | Author: Ethan Furman (ethan.furman)*![]() | Date: 2020-05-27 20:12 | |
New changesetb5ecbf02e4dbdea6d1c9a6d7189137f76e70c073 by Miss Islington (bot) in branch '3.8':bpo-40025: Require _generate_next_value_ to be defined before members(GH-19763)https://github.com/python/cpython/commit/b5ecbf02e4dbdea6d1c9a6d7189137f76e70c073 | |||
| msg371324 -(view) | Author: Ethan Furman (ethan.furman)*![]() | Date: 2020-06-11 21:48 | |
New changesetebd44003c9e206755e5e28716242ed8941495a62 by Miss Islington (bot) in branch '3.7':bpo-40025: Require _generate_next_value_ to be defined before members (GH-19762)https://github.com/python/cpython/commit/ebd44003c9e206755e5e28716242ed8941495a62 | |||
| msg372002 -(view) | Author: Srinivas Reddy Thatiparthy(శ్రీనివాస్ రెడ్డి తాటిపర్తి) (thatiparthy)* | Date: 2020-06-21 16:28 | |
We can close this? | |||
| msg372010 -(view) | Author: Ethan Furman (ethan.furman)*![]() | Date: 2020-06-21 18:16 | |
Not yet. I want to investigate the idea Ankesh Saha had some more. | |||
| msg377032 -(view) | Author: Ethan Furman (ethan.furman)*![]() | Date: 2020-09-17 00:32 | |
There was an effort to make it so `_generate_next_value_` could be defined last and still work correctly -- unfortunately, it could not handle the more common case of using `auto()` with the default `_generate_next_value_`: class I(Enum): first = auto() second = first + 2 # this line would failClosing the ticket. Thank you everyone! | |||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:28 | admin | set | github: 84206 |
| 2020-09-17 00:32:48 | ethan.furman | set | status: open -> closed resolution: fixed messages: +msg377032 stage: patch review -> resolved |
| 2020-09-17 00:26:45 | miss-islington | set | pull_requests: +pull_request21339 |
| 2020-09-17 00:26:27 | miss-islington | set | pull_requests: +pull_request21338 |
| 2020-09-16 18:33:31 | ethan.furman | set | pull_requests: +pull_request21337 |
| 2020-09-16 18:20:03 | ethan.furman | set | pull_requests: +pull_request21336 |
| 2020-06-21 18:16:32 | ethan.furman | set | messages: +msg372010 versions: + Python 3.10, - Python 3.7, Python 3.8, Python 3.9 |
| 2020-06-21 16:28:03 | thatiparthy | set | nosy: +thatiparthy messages: +msg372002 |
| 2020-06-11 21:48:55 | ethan.furman | set | messages: +msg371324 |
| 2020-05-27 20:12:15 | ethan.furman | set | messages: +msg370127 |
| 2020-05-04 16:37:26 | hongweipeng | set | nosy: +hongweipeng pull_requests: +pull_request19219 |
| 2020-04-28 17:22:53 | miss-islington | set | pull_requests: +pull_request19084 |
| 2020-04-28 17:22:44 | miss-islington | set | nosy: +miss-islington pull_requests: +pull_request19083 |
| 2020-04-28 17:21:03 | ethan.furman | set | messages: +msg367547 |
| 2020-04-28 15:51:20 | Ethan Onstott | set | messages: +msg367538 |
| 2020-03-30 15:23:41 | ankeshsaha | set | files: +Issue40025.PNG nosy: +ankeshsaha messages: +msg365321 |
| 2020-03-25 00:55:55 | Jonathan Hsu | set | messages: +msg364967 |
| 2020-03-22 05:20:26 | ethan.furman | set | messages: +msg364784 |
| 2020-03-21 21:45:08 | Jonathan Hsu | set | nosy: +Jonathan Hsu messages: +msg364776 |
| 2020-03-21 05:15:28 | Ethan Onstott | set | keywords: +patch nosy: +Ethan Onstott pull_requests: +pull_request18458 stage: needs patch -> patch review |
| 2020-03-20 19:09:21 | ethan.furman | set | versions: + Python 3.8, Python 3.9 messages: +msg364707 assignee:docs@python ->ethan.furman keywords: +easy stage: needs patch |
| 2020-03-20 13:35:17 | xtreak | set | nosy: +barry,eli.bendersky,ethan.furman |
| 2020-03-20 11:27:27 | edd07 | set | components: - Documentation |
| 2020-03-20 11:25:03 | edd07 | create | |