New submission from Chris Lambacher:

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.

----------
files: enum_value_transform.patch
keywords: patch
messages: 193648
nosy: lambacck
priority: normal
severity: normal
status: open
title: enum always runs member_type when use_args is True
type: behavior
versions: Python 3.4
Added file: http://bugs.python.org/file31028/enum_value_transform.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue18545>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to