mirror of
https://github.com/python/cpython.git
synced 2024-11-23 09:54:58 +08:00
gh-114149: [Enum] fix tuple subclass handling when using custom __new__ (GH-114160)
This commit is contained in:
parent
029ecee10d
commit
33b47a2c28
@ -250,7 +250,7 @@ class _proto_member:
|
||||
delattr(enum_class, member_name)
|
||||
# second step: create member based on enum_class
|
||||
value = self.value
|
||||
if not isinstance(value, tuple):
|
||||
if type(value) is not tuple:
|
||||
args = (value, )
|
||||
else:
|
||||
args = value
|
||||
@ -1777,7 +1777,7 @@ def _simple_enum(etype=Enum, *, boundary=None, use_args=None):
|
||||
else:
|
||||
# create the member
|
||||
if use_args:
|
||||
if not isinstance(value, tuple):
|
||||
if type(value) is not tuple:
|
||||
value = (value, )
|
||||
member = new_member(enum_class, *value)
|
||||
value = value[0]
|
||||
@ -1826,7 +1826,7 @@ def _simple_enum(etype=Enum, *, boundary=None, use_args=None):
|
||||
else:
|
||||
# create the member
|
||||
if use_args:
|
||||
if not isinstance(value, tuple):
|
||||
if type(value) is not tuple:
|
||||
value = (value, )
|
||||
member = new_member(enum_class, *value)
|
||||
value = value[0]
|
||||
|
@ -3201,6 +3201,22 @@ class TestSpecial(unittest.TestCase):
|
||||
[x.value for x in NTEnum],
|
||||
[TTuple(id=0, a=0, blist=[]), TTuple(id=1, a=2, blist=[4]), TTuple(id=2, a=4, blist=[0, 1, 2])],
|
||||
)
|
||||
#
|
||||
class NTDEnum(Enum):
|
||||
def __new__(cls, t_value):
|
||||
member = object.__new__(cls)
|
||||
member._value_ = t_value[0]
|
||||
member.id = t_value[0]
|
||||
member.a = t_value[1]
|
||||
member.blist = t_value[2]
|
||||
return member
|
||||
NONE = TTuple(0, 0, [])
|
||||
A = TTuple(1, 2, [4])
|
||||
B = TTuple(2, 4, [0, 1, 2])
|
||||
self.assertEqual(repr(NTDEnum.NONE), "<NTDEnum.NONE: 0>")
|
||||
self.assertEqual(NTDEnum.NONE.id, 0)
|
||||
self.assertEqual(NTDEnum.A.a, 2)
|
||||
self.assertEqual(NTDEnum.B.blist, [0, 1 ,2])
|
||||
|
||||
def test_flag_with_custom_new(self):
|
||||
class FlagFromChar(IntFlag):
|
||||
|
@ -0,0 +1 @@
|
||||
Enum: correctly handle tuple subclasses in custom ``__new__``.
|
Loading…
Reference in New Issue
Block a user