summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2020-12-10 00:41:22 (GMT)
committerGitHub <noreply@github.com>2020-12-10 00:41:22 (GMT)
commit6bd94de168b58ac9358277ed6f200490ab26c174 (patch)
tree01dfe448e2651e084d9d8e34eb7a301b2c6cc6c7 /Lib/enum.py
parent2a35137328154aa2513649dcf0bbef02c998e27c (diff)
downloadcpython-6bd94de168b58ac9358277ed6f200490ab26c174.zip
cpython-6bd94de168b58ac9358277ed6f200490ab26c174.tar.gz
cpython-6bd94de168b58ac9358277ed6f200490ab26c174.tar.bz2
bpo-42567: [Enum] call __init_subclass__ after members are added (GH-23714)
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.
Diffstat (limited to 'Lib/enum.py')
-rw-r--r--Lib/enum.py32
1 files changed, 30 insertions, 2 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index f6c7e8b..83e050e 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -9,6 +9,14 @@ __all__ = [
]
+class _NoInitSubclass:
+ """
+ temporary base class to suppress calling __init_subclass__
+ """
+ @classmethod
+ def __init_subclass__(cls, **kwds):
+ pass
+
def _is_descriptor(obj):
"""
Returns True if obj is a descriptor, False otherwise.
@@ -157,7 +165,7 @@ class EnumMeta(type):
)
return enum_dict
- def __new__(metacls, cls, bases, classdict):
+ def __new__(metacls, cls, bases, classdict, **kwds):
# an Enum class is final once enumeration items have been defined; it
# cannot be mixed with other types (int, float, etc.) if it has an
# inherited __new__ unless a new __new__ is defined (or the resulting
@@ -192,8 +200,22 @@ class EnumMeta(type):
if '__doc__' not in classdict:
classdict['__doc__'] = 'An enumeration.'
+ # postpone calling __init_subclass__
+ if '__init_subclass__' in classdict and classdict['__init_subclass__'] is None:
+ raise TypeError('%s.__init_subclass__ cannot be None')
+ # remove current __init_subclass__ so previous one can be found with getattr
+ new_init_subclass = classdict.pop('__init_subclass__', None)
# create our new Enum type
- enum_class = super().__new__(metacls, cls, bases, classdict)
+ if bases:
+ bases = (_NoInitSubclass, ) + bases
+ enum_class = type.__new__(metacls, cls, bases, classdict)
+ enum_class.__bases__ = enum_class.__bases__[1:] #or (object, )
+ else:
+ enum_class = type.__new__(metacls, cls, bases, classdict)
+ old_init_subclass = getattr(enum_class, '__init_subclass__', None)
+ # and restore the new one (if there was one)
+ if new_init_subclass is not None:
+ enum_class.__init_subclass__ = classmethod(new_init_subclass)
enum_class._member_names_ = [] # names in definition order
enum_class._member_map_ = {} # name->value map
enum_class._member_type_ = member_type
@@ -305,6 +327,9 @@ class EnumMeta(type):
if _order_ != enum_class._member_names_:
raise TypeError('member order does not match _order_')
+ # finally, call parents' __init_subclass__
+ if Enum is not None and old_init_subclass is not None:
+ old_init_subclass(**kwds)
return enum_class
def __bool__(self):
@@ -682,6 +707,9 @@ class Enum(metaclass=EnumMeta):
else:
return start
+ def __init_subclass__(cls, **kwds):
+ super().__init_subclass__(**kwds)
+
@classmethod
def _missing_(cls, value):
return None