diff --git a/Lib/enum.py b/Lib/enum.py index a8a50a58380..e51850d9b45 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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] diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index f99d4ca204b..48e0b02b2b6 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -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), "") + 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): diff --git a/Misc/NEWS.d/next/Library/2024-01-16-15-59-06.gh-issue-114149.LJ8IPm.rst b/Misc/NEWS.d/next/Library/2024-01-16-15-59-06.gh-issue-114149.LJ8IPm.rst new file mode 100644 index 00000000000..1403d78d0d4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-01-16-15-59-06.gh-issue-114149.LJ8IPm.rst @@ -0,0 +1 @@ +Enum: correctly handle tuple subclasses in custom ``__new__``.