Commit b41803e3 authored by Ethan Furman's avatar Ethan Furman

Close #18545: now only executes member_type if no _value_ is assigned in __new__.

parent 4d35e75c
......@@ -152,12 +152,12 @@ class EnumMeta(type):
args = (args, ) # wrap it one more time
if not use_args:
enum_member = __new__(enum_class)
original_value = value
if not hasattr(enum_member, '_value_'):
enum_member._value_ = value
else:
enum_member = __new__(enum_class, *args)
original_value = member_type(*args)
if not hasattr(enum_member, '_value_'):
enum_member._value_ = original_value
if not hasattr(enum_member, '_value_'):
enum_member._value_ = member_type(*args)
value = enum_member._value_
enum_member._member_type_ = member_type
enum_member._name_ = member_name
......
......@@ -934,6 +934,22 @@ class TestEnum(unittest.TestCase):
self.assertEqual(ColorInAList.red.value, [1])
self.assertEqual(ColorInAList([1]), ColorInAList.red)
def test_conflicting_types_resolved_in_new(self):
class LabelledIntEnum(int, Enum):
def __new__(cls, *args):
value, label = args
obj = int.__new__(cls, value)
obj.label = label
obj._value_ = value
return obj
class LabelledList(LabelledIntEnum):
unprocessed = (1, "Unprocessed")
payment_complete = (2, "Payment Complete")
self.assertEqual(list(LabelledList), [LabelledList.unprocessed, LabelledList.payment_complete])
self.assertEqual(LabelledList.unprocessed, 1)
self.assertEqual(LabelledList(1), LabelledList.unprocessed)
class TestUnique(unittest.TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment