https://github.com/python/cpython/commit/c1890e666eaaa819a318b4a6b4f2c8c33a8c679e
commit: c1890e666eaaa819a318b4a6b4f2c8c33a8c679e
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: ethanfurman <[email protected]>
date: 2024-01-17T13:34:48-08:00
summary:

[3.12] gh-114149: [Enum] fix tuple subclass handling when using custom __new__ 
(GH-114160) (GH-114196)

(cherry picked from commit 33b47a2c2853066b549f242065f6c2e12e18b33b)

Co-authored-by: Ethan Furman <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-01-16-15-59-06.gh-issue-114149.LJ8IPm.rst
M Lib/enum.py
M Lib/test/test_enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index 1502bfe9158520..0cf88471d504a2 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -254,7 +254,7 @@ def __set_name__(self, enum_class, member_name):
         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
@@ -1757,7 +1757,7 @@ def convert_class(cls):
                 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]
@@ -1807,7 +1807,7 @@ def convert_class(cls):
                 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 3bd918fb941c76..2db3b2f95fc845 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -3163,6 +3163,22 @@ class NTEnum(Enum):
                 [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):
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 00000000000000..1403d78d0d4905
--- /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__``.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to