diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2020-12-24 18:05:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-24 18:05:02 (GMT) |
commit | 6ec0adefad60ec7cdec61c44baecf1dccc1461ab (patch) | |
tree | 4063790397c109ba244d9c7615b351651512c401 | |
parent | 7ec59d8861ef1104c3028678b2cacde4c5693e19 (diff) | |
download | cpython-6ec0adefad60ec7cdec61c44baecf1dccc1461ab.zip cpython-6ec0adefad60ec7cdec61c44baecf1dccc1461ab.tar.gz cpython-6ec0adefad60ec7cdec61c44baecf1dccc1461ab.tar.bz2 |
[Enum] EnumMeta.__prepare__ now accepts **kwds (#23917)
-rw-r--r-- | Lib/enum.py | 2 | ||||
-rw-r--r-- | Lib/test/test_enum.py | 12 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-12-23-19-43-06.bpo-42727.WH3ODh.rst | 2 |
3 files changed, 14 insertions, 2 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 0070ebe..36a9a40 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -178,7 +178,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 a83241c..19b4fa5 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -2119,7 +2119,7 @@ class TestEnum(unittest.TestCase): one = '1' two = b'2', 'ascii', 9 - 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) @@ -2155,6 +2155,16 @@ class TestEnum(unittest.TestCase): 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) @unittest.skipUnless( sys.version_info[:2] == (3, 9), 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 0000000..a986cb9 --- /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__` |