This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: enum always runs member_type when use_args is True
Type: behavior Stage: resolved
Components: Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: ethan.furman, lambacck, python-dev
Priority: normal Keywords: patch

Created on 2013-07-24 14:01 by lambacck, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
enum_value_transform.patch lambacck, 2013-07-24 14:01 Patch with a test demonstrating the problem and a fix. review
Messages (2)
msg193648 - (view) Author: Chris Lambacher (lambacck) * Date: 2013-07-24 14:01
Starting at line 153 in enum.py there is:

   153             if not use_args:
   154                 enum_member = __new__(enum_class)
   155                 original_value = value
   156             else:
   157                 enum_member = __new__(enum_class, *args)
   158                 original_value = member_type(*args)
   159             if not hasattr(enum_member, '_value_'):
   160                 enum_member._value_ = original_value

When use_args is True, the member_type is always called with the arguments even if the return value from enum_member has a _value_ attr. If the __new__ function transforms the the *args then the call to member_type(*args) can fail even thought the value would not be used. I've attached a patch with a fix and a test to demonstrate the problem.

The use case for the test in the attached patch is to populate Django ORM choices to a field. The first value in the tuple is the value used in the database (and as the Enum value) and the second value in the tuple is the label that Django shows in select boxes.
msg193719 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-07-25 20:51
New changeset 95e1d0efd896 by Ethan Furman in branch 'default':
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
http://hg.python.org/cpython/rev/95e1d0efd896
History
Date User Action Args
2022-04-11 14:57:48adminsetgithub: 62745
2013-07-25 20:51:10python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg193719

resolution: fixed
stage: resolved
2013-07-24 15:12:52ethan.furmansetassignee: ethan.furman

nosy: + ethan.furman
2013-07-24 14:01:53lambacckcreate