mirror of
https://github.com/python/cpython.git
synced 2025-10-31 21:51:50 +00:00
[3.9] bpo-42567: [Enum] call __init_subclass__ after members are added (GH-23714) (GH-23772)
When creating an Enum, `type.__new__` calls `__init_subclass__`, but at that point the members have not been added.
This patch suppresses the initial call, then manually calls the ancestor `__init_subclass__` before returning the new Enum class.
(cherry picked from commit 6bd94de168)
This commit is contained in:
parent
aba12b67c1
commit
9d1fff1fcd
3 changed files with 103 additions and 3 deletions
|
|
@ -2049,7 +2049,6 @@ def test_empty_globals(self):
|
|||
local_ls = {}
|
||||
exec(code, global_ns, local_ls)
|
||||
|
||||
|
||||
@unittest.skipUnless(
|
||||
sys.version_info[:2] == (3, 9),
|
||||
'private variables are now normal attributes',
|
||||
|
|
@ -2066,6 +2065,42 @@ class Private(Enum):
|
|||
except ValueError:
|
||||
pass
|
||||
|
||||
def test_init_subclass(self):
|
||||
class MyEnum(Enum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
super(MyEnum, cls).__init_subclass__(**kwds)
|
||||
self.assertFalse(cls.__dict__.get('_test', False))
|
||||
cls._test1 = 'MyEnum'
|
||||
#
|
||||
class TheirEnum(MyEnum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
super().__init_subclass__(**kwds)
|
||||
cls._test2 = 'TheirEnum'
|
||||
class WhoseEnum(TheirEnum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
pass
|
||||
class NoEnum(WhoseEnum):
|
||||
ONE = 1
|
||||
self.assertEqual(TheirEnum.__dict__['_test1'], 'MyEnum')
|
||||
self.assertEqual(WhoseEnum.__dict__['_test1'], 'MyEnum')
|
||||
self.assertEqual(WhoseEnum.__dict__['_test2'], 'TheirEnum')
|
||||
self.assertFalse(NoEnum.__dict__.get('_test1', False))
|
||||
self.assertFalse(NoEnum.__dict__.get('_test2', False))
|
||||
#
|
||||
class OurEnum(MyEnum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
cls._test2 = 'OurEnum'
|
||||
class WhereEnum(OurEnum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
pass
|
||||
class NeverEnum(WhereEnum):
|
||||
ONE = 'one'
|
||||
self.assertEqual(OurEnum.__dict__['_test1'], 'MyEnum')
|
||||
self.assertFalse(WhereEnum.__dict__.get('_test1', False))
|
||||
self.assertEqual(WhereEnum.__dict__['_test2'], 'OurEnum')
|
||||
self.assertFalse(NeverEnum.__dict__.get('_test1', False))
|
||||
self.assertFalse(NeverEnum.__dict__.get('_test2', False))
|
||||
|
||||
|
||||
class TestOrder(unittest.TestCase):
|
||||
|
||||
|
|
@ -2516,6 +2551,42 @@ def cycle_enum():
|
|||
'at least one thread failed while creating composite members')
|
||||
self.assertEqual(256, len(seen), 'too many composite members created')
|
||||
|
||||
def test_init_subclass(self):
|
||||
class MyEnum(Flag):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
super().__init_subclass__(**kwds)
|
||||
self.assertFalse(cls.__dict__.get('_test', False))
|
||||
cls._test1 = 'MyEnum'
|
||||
#
|
||||
class TheirEnum(MyEnum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
super(TheirEnum, cls).__init_subclass__(**kwds)
|
||||
cls._test2 = 'TheirEnum'
|
||||
class WhoseEnum(TheirEnum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
pass
|
||||
class NoEnum(WhoseEnum):
|
||||
ONE = 1
|
||||
self.assertEqual(TheirEnum.__dict__['_test1'], 'MyEnum')
|
||||
self.assertEqual(WhoseEnum.__dict__['_test1'], 'MyEnum')
|
||||
self.assertEqual(WhoseEnum.__dict__['_test2'], 'TheirEnum')
|
||||
self.assertFalse(NoEnum.__dict__.get('_test1', False))
|
||||
self.assertFalse(NoEnum.__dict__.get('_test2', False))
|
||||
#
|
||||
class OurEnum(MyEnum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
cls._test2 = 'OurEnum'
|
||||
class WhereEnum(OurEnum):
|
||||
def __init_subclass__(cls, **kwds):
|
||||
pass
|
||||
class NeverEnum(WhereEnum):
|
||||
ONE = 1
|
||||
self.assertEqual(OurEnum.__dict__['_test1'], 'MyEnum')
|
||||
self.assertFalse(WhereEnum.__dict__.get('_test1', False))
|
||||
self.assertEqual(WhereEnum.__dict__['_test2'], 'OurEnum')
|
||||
self.assertFalse(NeverEnum.__dict__.get('_test1', False))
|
||||
self.assertFalse(NeverEnum.__dict__.get('_test2', False))
|
||||
|
||||
|
||||
class TestIntFlag(unittest.TestCase):
|
||||
"""Tests of the IntFlags."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue