[Python-checkins] [3.9] bpo-42727: [Enum] EnumMeta.__prepare__ now accepts **kwds (GH-23917). (GH-23926)

ethanfurman webhook-mailer at python.org
Thu Dec 24 15:03:04 EST 2020


https://github.com/python/cpython/commit/f7dca9b9c864c1b7807014ea21a30cac76727e8b
commit: f7dca9b9c864c1b7807014ea21a30cac76727e8b
branch: 3.9
author: Ethan Furman <ethan at stoneleaf.us>
committer: ethanfurman <ethan at stoneleaf.us>
date: 2020-12-24T12:02:38-08:00
summary:

[3.9] bpo-42727: [Enum] EnumMeta.__prepare__ now accepts **kwds (GH-23917). (GH-23926)

* [3.9] [Enum] EnumMeta.__prepare__ now accepts **kwds (GH-23917).
(cherry picked from commit 6ec0adefad60ec7cdec61c44baecf1dccc1461ab)

files:
A Misc/NEWS.d/next/Library/2020-12-23-19-43-06.bpo-42727.WH3ODh.rst
M Lib/enum.py
M Lib/test/test_enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index 88c951f4f12b4..aa9f4b00dd74d 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -170,7 +170,7 @@ class EnumMeta(type):
     Metaclass for Enum
     """
     @classmethod
-    def __prepare__(metacls, cls, bases):
+    def __prepare__(metacls, cls, bases, **kwds):
         # check that previous enum members do not exist
         metacls._check_for_existing_members(cls, bases)
         # create the namespace dict
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 0868c30347216..f6db73167dc95 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -2065,7 +2065,7 @@ class Private(Enum):
         except ValueError:
             pass
 
-    def test_init_subclass(self):
+    def test_init_subclass_calling(self):
         class MyEnum(Enum):
             def __init_subclass__(cls, **kwds):
                 super(MyEnum, cls).__init_subclass__(**kwds)
@@ -2101,6 +2101,16 @@ class NeverEnum(WhereEnum):
         self.assertFalse(NeverEnum.__dict__.get('_test1', False))
         self.assertFalse(NeverEnum.__dict__.get('_test2', False))
 
+    def test_init_subclass_parameter(self):
+        class multiEnum(Enum):
+            def __init_subclass__(cls, multi):
+                for member in cls:
+                    member._as_parameter_ = multi * member.value
+        class E(multiEnum, multi=3):
+            A = 1
+            B = 2
+        self.assertEqual(E.A._as_parameter_, 3)
+        self.assertEqual(E.B._as_parameter_, 6)
 
 class TestOrder(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Library/2020-12-23-19-43-06.bpo-42727.WH3ODh.rst b/Misc/NEWS.d/next/Library/2020-12-23-19-43-06.bpo-42727.WH3ODh.rst
new file mode 100644
index 0000000000000..a986cb960a79e
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-12-23-19-43-06.bpo-42727.WH3ODh.rst
@@ -0,0 +1,2 @@
+`EnumMeta.__prepare__` now accepts `**kwds` to properly support
+`__init_subclass__`



More information about the Python-checkins mailing list