Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: enum.py
import sys
[0] Fix | Delete
from types import MappingProxyType, DynamicClassAttribute
[1] Fix | Delete
from functools import reduce
[2] Fix | Delete
from operator import or_ as _or_
[3] Fix | Delete
[4] Fix | Delete
# try _collections first to reduce startup cost
[5] Fix | Delete
try:
[6] Fix | Delete
from _collections import OrderedDict
[7] Fix | Delete
except ImportError:
[8] Fix | Delete
from collections import OrderedDict
[9] Fix | Delete
[10] Fix | Delete
[11] Fix | Delete
__all__ = [
[12] Fix | Delete
'EnumMeta',
[13] Fix | Delete
'Enum', 'IntEnum', 'Flag', 'IntFlag',
[14] Fix | Delete
'auto', 'unique',
[15] Fix | Delete
]
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
def _is_descriptor(obj):
[19] Fix | Delete
"""Returns True if obj is a descriptor, False otherwise."""
[20] Fix | Delete
return (
[21] Fix | Delete
hasattr(obj, '__get__') or
[22] Fix | Delete
hasattr(obj, '__set__') or
[23] Fix | Delete
hasattr(obj, '__delete__'))
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
def _is_dunder(name):
[27] Fix | Delete
"""Returns True if a __dunder__ name, False otherwise."""
[28] Fix | Delete
return (name[:2] == name[-2:] == '__' and
[29] Fix | Delete
name[2:3] != '_' and
[30] Fix | Delete
name[-3:-2] != '_' and
[31] Fix | Delete
len(name) > 4)
[32] Fix | Delete
[33] Fix | Delete
[34] Fix | Delete
def _is_sunder(name):
[35] Fix | Delete
"""Returns True if a _sunder_ name, False otherwise."""
[36] Fix | Delete
return (name[0] == name[-1] == '_' and
[37] Fix | Delete
name[1:2] != '_' and
[38] Fix | Delete
name[-2:-1] != '_' and
[39] Fix | Delete
len(name) > 2)
[40] Fix | Delete
[41] Fix | Delete
def _make_class_unpicklable(cls):
[42] Fix | Delete
"""Make the given class un-picklable."""
[43] Fix | Delete
def _break_on_call_reduce(self, proto):
[44] Fix | Delete
raise TypeError('%r cannot be pickled' % self)
[45] Fix | Delete
cls.__reduce_ex__ = _break_on_call_reduce
[46] Fix | Delete
cls.__module__ = '<unknown>'
[47] Fix | Delete
[48] Fix | Delete
_auto_null = object()
[49] Fix | Delete
class auto:
[50] Fix | Delete
"""
[51] Fix | Delete
Instances are replaced with an appropriate value in Enum class suites.
[52] Fix | Delete
"""
[53] Fix | Delete
value = _auto_null
[54] Fix | Delete
[55] Fix | Delete
[56] Fix | Delete
class _EnumDict(dict):
[57] Fix | Delete
"""Track enum member order and ensure member names are not reused.
[58] Fix | Delete
[59] Fix | Delete
EnumMeta will use the names found in self._member_names as the
[60] Fix | Delete
enumeration member names.
[61] Fix | Delete
[62] Fix | Delete
"""
[63] Fix | Delete
def __init__(self):
[64] Fix | Delete
super().__init__()
[65] Fix | Delete
self._member_names = []
[66] Fix | Delete
self._last_values = []
[67] Fix | Delete
[68] Fix | Delete
def __setitem__(self, key, value):
[69] Fix | Delete
"""Changes anything not dundered or not a descriptor.
[70] Fix | Delete
[71] Fix | Delete
If an enum member name is used twice, an error is raised; duplicate
[72] Fix | Delete
values are not checked for.
[73] Fix | Delete
[74] Fix | Delete
Single underscore (sunder) names are reserved.
[75] Fix | Delete
[76] Fix | Delete
"""
[77] Fix | Delete
if _is_sunder(key):
[78] Fix | Delete
if key not in (
[79] Fix | Delete
'_order_', '_create_pseudo_member_',
[80] Fix | Delete
'_generate_next_value_', '_missing_',
[81] Fix | Delete
):
[82] Fix | Delete
raise ValueError('_names_ are reserved for future Enum use')
[83] Fix | Delete
if key == '_generate_next_value_':
[84] Fix | Delete
setattr(self, '_generate_next_value', value)
[85] Fix | Delete
elif _is_dunder(key):
[86] Fix | Delete
if key == '__order__':
[87] Fix | Delete
key = '_order_'
[88] Fix | Delete
elif key in self._member_names:
[89] Fix | Delete
# descriptor overwriting an enum?
[90] Fix | Delete
raise TypeError('Attempted to reuse key: %r' % key)
[91] Fix | Delete
elif not _is_descriptor(value):
[92] Fix | Delete
if key in self:
[93] Fix | Delete
# enum overwriting a descriptor?
[94] Fix | Delete
raise TypeError('%r already defined as: %r' % (key, self[key]))
[95] Fix | Delete
if isinstance(value, auto):
[96] Fix | Delete
if value.value == _auto_null:
[97] Fix | Delete
value.value = self._generate_next_value(key, 1, len(self._member_names), self._last_values[:])
[98] Fix | Delete
value = value.value
[99] Fix | Delete
self._member_names.append(key)
[100] Fix | Delete
self._last_values.append(value)
[101] Fix | Delete
super().__setitem__(key, value)
[102] Fix | Delete
[103] Fix | Delete
[104] Fix | Delete
# Dummy value for Enum as EnumMeta explicitly checks for it, but of course
[105] Fix | Delete
# until EnumMeta finishes running the first time the Enum class doesn't exist.
[106] Fix | Delete
# This is also why there are checks in EnumMeta like `if Enum is not None`
[107] Fix | Delete
Enum = None
[108] Fix | Delete
[109] Fix | Delete
[110] Fix | Delete
class EnumMeta(type):
[111] Fix | Delete
"""Metaclass for Enum"""
[112] Fix | Delete
@classmethod
[113] Fix | Delete
def __prepare__(metacls, cls, bases):
[114] Fix | Delete
# create the namespace dict
[115] Fix | Delete
enum_dict = _EnumDict()
[116] Fix | Delete
# inherit previous flags and _generate_next_value_ function
[117] Fix | Delete
member_type, first_enum = metacls._get_mixins_(bases)
[118] Fix | Delete
if first_enum is not None:
[119] Fix | Delete
enum_dict['_generate_next_value_'] = getattr(first_enum, '_generate_next_value_', None)
[120] Fix | Delete
return enum_dict
[121] Fix | Delete
[122] Fix | Delete
def __new__(metacls, cls, bases, classdict):
[123] Fix | Delete
# an Enum class is final once enumeration items have been defined; it
[124] Fix | Delete
# cannot be mixed with other types (int, float, etc.) if it has an
[125] Fix | Delete
# inherited __new__ unless a new __new__ is defined (or the resulting
[126] Fix | Delete
# class will fail).
[127] Fix | Delete
member_type, first_enum = metacls._get_mixins_(bases)
[128] Fix | Delete
__new__, save_new, use_args = metacls._find_new_(classdict, member_type,
[129] Fix | Delete
first_enum)
[130] Fix | Delete
[131] Fix | Delete
# save enum items into separate mapping so they don't get baked into
[132] Fix | Delete
# the new class
[133] Fix | Delete
enum_members = {k: classdict[k] for k in classdict._member_names}
[134] Fix | Delete
for name in classdict._member_names:
[135] Fix | Delete
del classdict[name]
[136] Fix | Delete
[137] Fix | Delete
# adjust the sunders
[138] Fix | Delete
_order_ = classdict.pop('_order_', None)
[139] Fix | Delete
[140] Fix | Delete
# check for illegal enum names (any others?)
[141] Fix | Delete
invalid_names = set(enum_members) & {'mro', }
[142] Fix | Delete
if invalid_names:
[143] Fix | Delete
raise ValueError('Invalid enum member name: {0}'.format(
[144] Fix | Delete
','.join(invalid_names)))
[145] Fix | Delete
[146] Fix | Delete
# create a default docstring if one has not been provided
[147] Fix | Delete
if '__doc__' not in classdict:
[148] Fix | Delete
classdict['__doc__'] = 'An enumeration.'
[149] Fix | Delete
[150] Fix | Delete
# create our new Enum type
[151] Fix | Delete
enum_class = super().__new__(metacls, cls, bases, classdict)
[152] Fix | Delete
enum_class._member_names_ = [] # names in definition order
[153] Fix | Delete
enum_class._member_map_ = OrderedDict() # name->value map
[154] Fix | Delete
enum_class._member_type_ = member_type
[155] Fix | Delete
[156] Fix | Delete
# save DynamicClassAttribute attributes from super classes so we know
[157] Fix | Delete
# if we can take the shortcut of storing members in the class dict
[158] Fix | Delete
dynamic_attributes = {k for c in enum_class.mro()
[159] Fix | Delete
for k, v in c.__dict__.items()
[160] Fix | Delete
if isinstance(v, DynamicClassAttribute)}
[161] Fix | Delete
[162] Fix | Delete
# Reverse value->name map for hashable values.
[163] Fix | Delete
enum_class._value2member_map_ = {}
[164] Fix | Delete
[165] Fix | Delete
# If a custom type is mixed into the Enum, and it does not know how
[166] Fix | Delete
# to pickle itself, pickle.dumps will succeed but pickle.loads will
[167] Fix | Delete
# fail. Rather than have the error show up later and possibly far
[168] Fix | Delete
# from the source, sabotage the pickle protocol for this class so
[169] Fix | Delete
# that pickle.dumps also fails.
[170] Fix | Delete
#
[171] Fix | Delete
# However, if the new class implements its own __reduce_ex__, do not
[172] Fix | Delete
# sabotage -- it's on them to make sure it works correctly. We use
[173] Fix | Delete
# __reduce_ex__ instead of any of the others as it is preferred by
[174] Fix | Delete
# pickle over __reduce__, and it handles all pickle protocols.
[175] Fix | Delete
if '__reduce_ex__' not in classdict:
[176] Fix | Delete
if member_type is not object:
[177] Fix | Delete
methods = ('__getnewargs_ex__', '__getnewargs__',
[178] Fix | Delete
'__reduce_ex__', '__reduce__')
[179] Fix | Delete
if not any(m in member_type.__dict__ for m in methods):
[180] Fix | Delete
_make_class_unpicklable(enum_class)
[181] Fix | Delete
[182] Fix | Delete
# instantiate them, checking for duplicates as we go
[183] Fix | Delete
# we instantiate first instead of checking for duplicates first in case
[184] Fix | Delete
# a custom __new__ is doing something funky with the values -- such as
[185] Fix | Delete
# auto-numbering ;)
[186] Fix | Delete
for member_name in classdict._member_names:
[187] Fix | Delete
value = enum_members[member_name]
[188] Fix | Delete
if not isinstance(value, tuple):
[189] Fix | Delete
args = (value, )
[190] Fix | Delete
else:
[191] Fix | Delete
args = value
[192] Fix | Delete
if member_type is tuple: # special case for tuple enums
[193] Fix | Delete
args = (args, ) # wrap it one more time
[194] Fix | Delete
if not use_args:
[195] Fix | Delete
enum_member = __new__(enum_class)
[196] Fix | Delete
if not hasattr(enum_member, '_value_'):
[197] Fix | Delete
enum_member._value_ = value
[198] Fix | Delete
else:
[199] Fix | Delete
enum_member = __new__(enum_class, *args)
[200] Fix | Delete
if not hasattr(enum_member, '_value_'):
[201] Fix | Delete
if member_type is object:
[202] Fix | Delete
enum_member._value_ = value
[203] Fix | Delete
else:
[204] Fix | Delete
enum_member._value_ = member_type(*args)
[205] Fix | Delete
value = enum_member._value_
[206] Fix | Delete
enum_member._name_ = member_name
[207] Fix | Delete
enum_member.__objclass__ = enum_class
[208] Fix | Delete
enum_member.__init__(*args)
[209] Fix | Delete
# If another member with the same value was already defined, the
[210] Fix | Delete
# new member becomes an alias to the existing one.
[211] Fix | Delete
for name, canonical_member in enum_class._member_map_.items():
[212] Fix | Delete
if canonical_member._value_ == enum_member._value_:
[213] Fix | Delete
enum_member = canonical_member
[214] Fix | Delete
break
[215] Fix | Delete
else:
[216] Fix | Delete
# Aliases don't appear in member names (only in __members__).
[217] Fix | Delete
enum_class._member_names_.append(member_name)
[218] Fix | Delete
# performance boost for any member that would not shadow
[219] Fix | Delete
# a DynamicClassAttribute
[220] Fix | Delete
if member_name not in dynamic_attributes:
[221] Fix | Delete
setattr(enum_class, member_name, enum_member)
[222] Fix | Delete
# now add to _member_map_
[223] Fix | Delete
enum_class._member_map_[member_name] = enum_member
[224] Fix | Delete
try:
[225] Fix | Delete
# This may fail if value is not hashable. We can't add the value
[226] Fix | Delete
# to the map, and by-value lookups for this value will be
[227] Fix | Delete
# linear.
[228] Fix | Delete
enum_class._value2member_map_[value] = enum_member
[229] Fix | Delete
except TypeError:
[230] Fix | Delete
pass
[231] Fix | Delete
[232] Fix | Delete
# double check that repr and friends are not the mixin's or various
[233] Fix | Delete
# things break (such as pickle)
[234] Fix | Delete
for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'):
[235] Fix | Delete
class_method = getattr(enum_class, name)
[236] Fix | Delete
obj_method = getattr(member_type, name, None)
[237] Fix | Delete
enum_method = getattr(first_enum, name, None)
[238] Fix | Delete
if obj_method is not None and obj_method is class_method:
[239] Fix | Delete
setattr(enum_class, name, enum_method)
[240] Fix | Delete
[241] Fix | Delete
# replace any other __new__ with our own (as long as Enum is not None,
[242] Fix | Delete
# anyway) -- again, this is to support pickle
[243] Fix | Delete
if Enum is not None:
[244] Fix | Delete
# if the user defined their own __new__, save it before it gets
[245] Fix | Delete
# clobbered in case they subclass later
[246] Fix | Delete
if save_new:
[247] Fix | Delete
enum_class.__new_member__ = __new__
[248] Fix | Delete
enum_class.__new__ = Enum.__new__
[249] Fix | Delete
[250] Fix | Delete
# py3 support for definition order (helps keep py2/py3 code in sync)
[251] Fix | Delete
if _order_ is not None:
[252] Fix | Delete
if isinstance(_order_, str):
[253] Fix | Delete
_order_ = _order_.replace(',', ' ').split()
[254] Fix | Delete
if _order_ != enum_class._member_names_:
[255] Fix | Delete
raise TypeError('member order does not match _order_')
[256] Fix | Delete
[257] Fix | Delete
return enum_class
[258] Fix | Delete
[259] Fix | Delete
def __bool__(self):
[260] Fix | Delete
"""
[261] Fix | Delete
classes/types should always be True.
[262] Fix | Delete
"""
[263] Fix | Delete
return True
[264] Fix | Delete
[265] Fix | Delete
def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1):
[266] Fix | Delete
"""Either returns an existing member, or creates a new enum class.
[267] Fix | Delete
[268] Fix | Delete
This method is used both when an enum class is given a value to match
[269] Fix | Delete
to an enumeration member (i.e. Color(3)) and for the functional API
[270] Fix | Delete
(i.e. Color = Enum('Color', names='RED GREEN BLUE')).
[271] Fix | Delete
[272] Fix | Delete
When used for the functional API:
[273] Fix | Delete
[274] Fix | Delete
`value` will be the name of the new class.
[275] Fix | Delete
[276] Fix | Delete
`names` should be either a string of white-space/comma delimited names
[277] Fix | Delete
(values will start at `start`), or an iterator/mapping of name, value pairs.
[278] Fix | Delete
[279] Fix | Delete
`module` should be set to the module this class is being created in;
[280] Fix | Delete
if it is not set, an attempt to find that module will be made, but if
[281] Fix | Delete
it fails the class will not be picklable.
[282] Fix | Delete
[283] Fix | Delete
`qualname` should be set to the actual location this class can be found
[284] Fix | Delete
at in its module; by default it is set to the global scope. If this is
[285] Fix | Delete
not correct, unpickling will fail in some circumstances.
[286] Fix | Delete
[287] Fix | Delete
`type`, if set, will be mixed in as the first base class.
[288] Fix | Delete
[289] Fix | Delete
"""
[290] Fix | Delete
if names is None: # simple value lookup
[291] Fix | Delete
return cls.__new__(cls, value)
[292] Fix | Delete
# otherwise, functional API: we're creating a new Enum type
[293] Fix | Delete
return cls._create_(value, names, module=module, qualname=qualname, type=type, start=start)
[294] Fix | Delete
[295] Fix | Delete
def __contains__(cls, member):
[296] Fix | Delete
return isinstance(member, cls) and member._name_ in cls._member_map_
[297] Fix | Delete
[298] Fix | Delete
def __delattr__(cls, attr):
[299] Fix | Delete
# nicer error message when someone tries to delete an attribute
[300] Fix | Delete
# (see issue19025).
[301] Fix | Delete
if attr in cls._member_map_:
[302] Fix | Delete
raise AttributeError(
[303] Fix | Delete
"%s: cannot delete Enum member." % cls.__name__)
[304] Fix | Delete
super().__delattr__(attr)
[305] Fix | Delete
[306] Fix | Delete
def __dir__(self):
[307] Fix | Delete
return (['__class__', '__doc__', '__members__', '__module__'] +
[308] Fix | Delete
self._member_names_)
[309] Fix | Delete
[310] Fix | Delete
def __getattr__(cls, name):
[311] Fix | Delete
"""Return the enum member matching `name`
[312] Fix | Delete
[313] Fix | Delete
We use __getattr__ instead of descriptors or inserting into the enum
[314] Fix | Delete
class' __dict__ in order to support `name` and `value` being both
[315] Fix | Delete
properties for enum members (which live in the class' __dict__) and
[316] Fix | Delete
enum members themselves.
[317] Fix | Delete
[318] Fix | Delete
"""
[319] Fix | Delete
if _is_dunder(name):
[320] Fix | Delete
raise AttributeError(name)
[321] Fix | Delete
try:
[322] Fix | Delete
return cls._member_map_[name]
[323] Fix | Delete
except KeyError:
[324] Fix | Delete
raise AttributeError(name) from None
[325] Fix | Delete
[326] Fix | Delete
def __getitem__(cls, name):
[327] Fix | Delete
return cls._member_map_[name]
[328] Fix | Delete
[329] Fix | Delete
def __iter__(cls):
[330] Fix | Delete
return (cls._member_map_[name] for name in cls._member_names_)
[331] Fix | Delete
[332] Fix | Delete
def __len__(cls):
[333] Fix | Delete
return len(cls._member_names_)
[334] Fix | Delete
[335] Fix | Delete
@property
[336] Fix | Delete
def __members__(cls):
[337] Fix | Delete
"""Returns a mapping of member name->value.
[338] Fix | Delete
[339] Fix | Delete
This mapping lists all enum members, including aliases. Note that this
[340] Fix | Delete
is a read-only view of the internal mapping.
[341] Fix | Delete
[342] Fix | Delete
"""
[343] Fix | Delete
return MappingProxyType(cls._member_map_)
[344] Fix | Delete
[345] Fix | Delete
def __repr__(cls):
[346] Fix | Delete
return "<enum %r>" % cls.__name__
[347] Fix | Delete
[348] Fix | Delete
def __reversed__(cls):
[349] Fix | Delete
return (cls._member_map_[name] for name in reversed(cls._member_names_))
[350] Fix | Delete
[351] Fix | Delete
def __setattr__(cls, name, value):
[352] Fix | Delete
"""Block attempts to reassign Enum members.
[353] Fix | Delete
[354] Fix | Delete
A simple assignment to the class namespace only changes one of the
[355] Fix | Delete
several possible ways to get an Enum member from the Enum class,
[356] Fix | Delete
resulting in an inconsistent Enumeration.
[357] Fix | Delete
[358] Fix | Delete
"""
[359] Fix | Delete
member_map = cls.__dict__.get('_member_map_', {})
[360] Fix | Delete
if name in member_map:
[361] Fix | Delete
raise AttributeError('Cannot reassign members.')
[362] Fix | Delete
super().__setattr__(name, value)
[363] Fix | Delete
[364] Fix | Delete
def _create_(cls, class_name, names, *, module=None, qualname=None, type=None, start=1):
[365] Fix | Delete
"""Convenience method to create a new Enum class.
[366] Fix | Delete
[367] Fix | Delete
`names` can be:
[368] Fix | Delete
[369] Fix | Delete
* A string containing member names, separated either with spaces or
[370] Fix | Delete
commas. Values are incremented by 1 from `start`.
[371] Fix | Delete
* An iterable of member names. Values are incremented by 1 from `start`.
[372] Fix | Delete
* An iterable of (member name, value) pairs.
[373] Fix | Delete
* A mapping of member name -> value pairs.
[374] Fix | Delete
[375] Fix | Delete
"""
[376] Fix | Delete
metacls = cls.__class__
[377] Fix | Delete
bases = (cls, ) if type is None else (type, cls)
[378] Fix | Delete
_, first_enum = cls._get_mixins_(bases)
[379] Fix | Delete
classdict = metacls.__prepare__(class_name, bases)
[380] Fix | Delete
[381] Fix | Delete
# special processing needed for names?
[382] Fix | Delete
if isinstance(names, str):
[383] Fix | Delete
names = names.replace(',', ' ').split()
[384] Fix | Delete
if isinstance(names, (tuple, list)) and names and isinstance(names[0], str):
[385] Fix | Delete
original_names, names = names, []
[386] Fix | Delete
last_values = []
[387] Fix | Delete
for count, name in enumerate(original_names):
[388] Fix | Delete
value = first_enum._generate_next_value_(name, start, count, last_values[:])
[389] Fix | Delete
last_values.append(value)
[390] Fix | Delete
names.append((name, value))
[391] Fix | Delete
[392] Fix | Delete
# Here, names is either an iterable of (name, value) or a mapping.
[393] Fix | Delete
for item in names:
[394] Fix | Delete
if isinstance(item, str):
[395] Fix | Delete
member_name, member_value = item, names[item]
[396] Fix | Delete
else:
[397] Fix | Delete
member_name, member_value = item
[398] Fix | Delete
classdict[member_name] = member_value
[399] Fix | Delete
enum_class = metacls.__new__(metacls, class_name, bases, classdict)
[400] Fix | Delete
[401] Fix | Delete
# TODO: replace the frame hack if a blessed way to know the calling
[402] Fix | Delete
# module is ever developed
[403] Fix | Delete
if module is None:
[404] Fix | Delete
try:
[405] Fix | Delete
module = sys._getframe(2).f_globals['__name__']
[406] Fix | Delete
except (AttributeError, ValueError) as exc:
[407] Fix | Delete
pass
[408] Fix | Delete
if module is None:
[409] Fix | Delete
_make_class_unpicklable(enum_class)
[410] Fix | Delete
else:
[411] Fix | Delete
enum_class.__module__ = module
[412] Fix | Delete
if qualname is not None:
[413] Fix | Delete
enum_class.__qualname__ = qualname
[414] Fix | Delete
[415] Fix | Delete
return enum_class
[416] Fix | Delete
[417] Fix | Delete
@staticmethod
[418] Fix | Delete
def _get_mixins_(bases):
[419] Fix | Delete
"""Returns the type for creating enum members, and the first inherited
[420] Fix | Delete
enum class.
[421] Fix | Delete
[422] Fix | Delete
bases: the tuple of bases that was given to __new__
[423] Fix | Delete
[424] Fix | Delete
"""
[425] Fix | Delete
if not bases:
[426] Fix | Delete
return object, Enum
[427] Fix | Delete
[428] Fix | Delete
# double check that we are not subclassing a class with existing
[429] Fix | Delete
# enumeration members; while we're at it, see if any other data
[430] Fix | Delete
# type has been mixed in so we can use the correct __new__
[431] Fix | Delete
member_type = first_enum = None
[432] Fix | Delete
for base in bases:
[433] Fix | Delete
if (base is not Enum and
[434] Fix | Delete
issubclass(base, Enum) and
[435] Fix | Delete
base._member_names_):
[436] Fix | Delete
raise TypeError("Cannot extend enumerations")
[437] Fix | Delete
# base is now the last base in bases
[438] Fix | Delete
if not issubclass(base, Enum):
[439] Fix | Delete
raise TypeError("new enumerations must be created as "
[440] Fix | Delete
"`ClassName([mixin_type,] enum_type)`")
[441] Fix | Delete
[442] Fix | Delete
# get correct mix-in type (either mix-in type of Enum subclass, or
[443] Fix | Delete
# first base if last base is Enum)
[444] Fix | Delete
if not issubclass(bases[0], Enum):
[445] Fix | Delete
member_type = bases[0] # first data type
[446] Fix | Delete
first_enum = bases[-1] # enum type
[447] Fix | Delete
else:
[448] Fix | Delete
for base in bases[0].__mro__:
[449] Fix | Delete
# most common: (IntEnum, int, Enum, object)
[450] Fix | Delete
# possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,
[451] Fix | Delete
# <class 'int'>, <Enum 'Enum'>,
[452] Fix | Delete
# <class 'object'>)
[453] Fix | Delete
if issubclass(base, Enum):
[454] Fix | Delete
if first_enum is None:
[455] Fix | Delete
first_enum = base
[456] Fix | Delete
else:
[457] Fix | Delete
if member_type is None:
[458] Fix | Delete
member_type = base
[459] Fix | Delete
[460] Fix | Delete
return member_type, first_enum
[461] Fix | Delete
[462] Fix | Delete
@staticmethod
[463] Fix | Delete
def _find_new_(classdict, member_type, first_enum):
[464] Fix | Delete
"""Returns the __new__ to be used for creating the enum members.
[465] Fix | Delete
[466] Fix | Delete
classdict: the class dictionary given to __new__
[467] Fix | Delete
member_type: the data type whose __new__ will be used by default
[468] Fix | Delete
first_enum: enumeration to check for an overriding __new__
[469] Fix | Delete
[470] Fix | Delete
"""
[471] Fix | Delete
# now find the correct __new__, checking to see of one was defined
[472] Fix | Delete
# by the user; also check earlier enum classes in case a __new__ was
[473] Fix | Delete
# saved as __new_member__
[474] Fix | Delete
__new__ = classdict.get('__new__', None)
[475] Fix | Delete
[476] Fix | Delete
# should __new__ be saved as __new_member__ later?
[477] Fix | Delete
save_new = __new__ is not None
[478] Fix | Delete
[479] Fix | Delete
if __new__ is None:
[480] Fix | Delete
# check all possibles for __new_member__ before falling back to
[481] Fix | Delete
# __new__
[482] Fix | Delete
for method in ('__new_member__', '__new__'):
[483] Fix | Delete
for possible in (member_type, first_enum):
[484] Fix | Delete
target = getattr(possible, method, None)
[485] Fix | Delete
if target not in {
[486] Fix | Delete
None,
[487] Fix | Delete
None.__new__,
[488] Fix | Delete
object.__new__,
[489] Fix | Delete
Enum.__new__,
[490] Fix | Delete
}:
[491] Fix | Delete
__new__ = target
[492] Fix | Delete
break
[493] Fix | Delete
if __new__ is not None:
[494] Fix | Delete
break
[495] Fix | Delete
else:
[496] Fix | Delete
__new__ = object.__new__
[497] Fix | Delete
[498] Fix | Delete
# if a non-object.__new__ is used then whatever value/tuple was
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function