Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 1 | import sys |
| 2 | from collections import OrderedDict |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 3 | from types import MappingProxyType, DynamicClassAttribute |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 4 | |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 5 | __all__ = ['Enum', 'IntEnum', 'unique'] |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 6 | |
| 7 | |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 8 | def _is_descriptor(obj): |
| 9 | """Returns True if obj is a descriptor, False otherwise.""" |
| 10 | return ( |
| 11 | hasattr(obj, '__get__') or |
| 12 | hasattr(obj, '__set__') or |
| 13 | hasattr(obj, '__delete__')) |
| 14 | |
| 15 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 16 | def _is_dunder(name): |
| 17 | """Returns True if a __dunder__ name, False otherwise.""" |
| 18 | return (name[:2] == name[-2:] == '__' and |
| 19 | name[2:3] != '_' and |
Ethan Furman | 648f860 | 2013-10-06 17:19:54 -0700 | [diff] [blame] | 20 | name[-3:-2] != '_' and |
| 21 | len(name) > 4) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def _is_sunder(name): |
| 25 | """Returns True if a _sunder_ name, False otherwise.""" |
| 26 | return (name[0] == name[-1] == '_' and |
| 27 | name[1:2] != '_' and |
Ethan Furman | 648f860 | 2013-10-06 17:19:54 -0700 | [diff] [blame] | 28 | name[-2:-1] != '_' and |
| 29 | len(name) > 2) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def _make_class_unpicklable(cls): |
| 33 | """Make the given class un-picklable.""" |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 34 | def _break_on_call_reduce(self, proto): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 35 | raise TypeError('%r cannot be pickled' % self) |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 36 | cls.__reduce_ex__ = _break_on_call_reduce |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 37 | cls.__module__ = '<unknown>' |
| 38 | |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 39 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 40 | class _EnumDict(dict): |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 41 | """Track enum member order and ensure member names are not reused. |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 42 | |
| 43 | EnumMeta will use the names found in self._member_names as the |
| 44 | enumeration member names. |
| 45 | |
| 46 | """ |
| 47 | def __init__(self): |
| 48 | super().__init__() |
| 49 | self._member_names = [] |
| 50 | |
| 51 | def __setitem__(self, key, value): |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 52 | """Changes anything not dundered or not a descriptor. |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 53 | |
| 54 | If an enum member name is used twice, an error is raised; duplicate |
| 55 | values are not checked for. |
| 56 | |
| 57 | Single underscore (sunder) names are reserved. |
| 58 | |
| 59 | """ |
| 60 | if _is_sunder(key): |
| 61 | raise ValueError('_names_ are reserved for future Enum use') |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 62 | elif _is_dunder(key): |
| 63 | pass |
| 64 | elif key in self._member_names: |
| 65 | # descriptor overwriting an enum? |
| 66 | raise TypeError('Attempted to reuse key: %r' % key) |
| 67 | elif not _is_descriptor(value): |
| 68 | if key in self: |
| 69 | # enum overwriting a descriptor? |
| 70 | raise TypeError('Key already defined as: %r' % self[key]) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 71 | self._member_names.append(key) |
| 72 | super().__setitem__(key, value) |
| 73 | |
| 74 | |
Ethan Furman | 101e074 | 2013-09-15 12:34:36 -0700 | [diff] [blame] | 75 | |
Ezio Melotti | 9a3777e | 2013-08-17 15:53:55 +0300 | [diff] [blame] | 76 | # Dummy value for Enum as EnumMeta explicitly checks for it, but of course |
| 77 | # until EnumMeta finishes running the first time the Enum class doesn't exist. |
| 78 | # This is also why there are checks in EnumMeta like `if Enum is not None` |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 79 | Enum = None |
| 80 | |
| 81 | |
| 82 | class EnumMeta(type): |
| 83 | """Metaclass for Enum""" |
| 84 | @classmethod |
| 85 | def __prepare__(metacls, cls, bases): |
| 86 | return _EnumDict() |
| 87 | |
| 88 | def __new__(metacls, cls, bases, classdict): |
| 89 | # an Enum class is final once enumeration items have been defined; it |
| 90 | # cannot be mixed with other types (int, float, etc.) if it has an |
| 91 | # inherited __new__ unless a new __new__ is defined (or the resulting |
| 92 | # class will fail). |
| 93 | member_type, first_enum = metacls._get_mixins_(bases) |
| 94 | __new__, save_new, use_args = metacls._find_new_(classdict, member_type, |
| 95 | first_enum) |
| 96 | |
| 97 | # save enum items into separate mapping so they don't get baked into |
| 98 | # the new class |
| 99 | members = {k: classdict[k] for k in classdict._member_names} |
| 100 | for name in classdict._member_names: |
| 101 | del classdict[name] |
| 102 | |
| 103 | # check for illegal enum names (any others?) |
| 104 | invalid_names = set(members) & {'mro', } |
| 105 | if invalid_names: |
| 106 | raise ValueError('Invalid enum member name: {0}'.format( |
| 107 | ','.join(invalid_names))) |
| 108 | |
Ethan Furman | 48a724f | 2015-04-11 23:23:06 -0700 | [diff] [blame^] | 109 | # create a default docstring if one has not been provided |
| 110 | if '__doc__' not in classdict: |
| 111 | classdict['__doc__'] = 'An enumeration.' |
| 112 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 113 | # create our new Enum type |
| 114 | enum_class = super().__new__(metacls, cls, bases, classdict) |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 115 | enum_class._member_names_ = [] # names in definition order |
| 116 | enum_class._member_map_ = OrderedDict() # name->value map |
Ethan Furman | 5e5a823 | 2013-08-04 08:42:23 -0700 | [diff] [blame] | 117 | enum_class._member_type_ = member_type |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 118 | |
Ethan Furman | 354ecf1 | 2015-03-11 08:43:12 -0700 | [diff] [blame] | 119 | # save attributes from super classes so we know if we can take |
| 120 | # the shortcut of storing members in the class dict |
| 121 | base_attributes = {a for b in bases for a in b.__dict__} |
| 122 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 123 | # Reverse value->name map for hashable values. |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 124 | enum_class._value2member_map_ = {} |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 125 | |
Ethan Furman | 2da9504 | 2014-03-03 12:42:52 -0800 | [diff] [blame] | 126 | # If a custom type is mixed into the Enum, and it does not know how |
| 127 | # to pickle itself, pickle.dumps will succeed but pickle.loads will |
| 128 | # fail. Rather than have the error show up later and possibly far |
| 129 | # from the source, sabotage the pickle protocol for this class so |
| 130 | # that pickle.dumps also fails. |
| 131 | # |
| 132 | # However, if the new class implements its own __reduce_ex__, do not |
| 133 | # sabotage -- it's on them to make sure it works correctly. We use |
| 134 | # __reduce_ex__ instead of any of the others as it is preferred by |
| 135 | # pickle over __reduce__, and it handles all pickle protocols. |
| 136 | if '__reduce_ex__' not in classdict: |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 137 | if member_type is not object: |
| 138 | methods = ('__getnewargs_ex__', '__getnewargs__', |
| 139 | '__reduce_ex__', '__reduce__') |
Ethan Furman | 2da9504 | 2014-03-03 12:42:52 -0800 | [diff] [blame] | 140 | if not any(m in member_type.__dict__ for m in methods): |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 141 | _make_class_unpicklable(enum_class) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 142 | |
| 143 | # instantiate them, checking for duplicates as we go |
| 144 | # we instantiate first instead of checking for duplicates first in case |
| 145 | # a custom __new__ is doing something funky with the values -- such as |
| 146 | # auto-numbering ;) |
| 147 | for member_name in classdict._member_names: |
| 148 | value = members[member_name] |
| 149 | if not isinstance(value, tuple): |
| 150 | args = (value, ) |
| 151 | else: |
| 152 | args = value |
| 153 | if member_type is tuple: # special case for tuple enums |
| 154 | args = (args, ) # wrap it one more time |
| 155 | if not use_args: |
| 156 | enum_member = __new__(enum_class) |
Ethan Furman | b41803e | 2013-07-25 13:50:45 -0700 | [diff] [blame] | 157 | if not hasattr(enum_member, '_value_'): |
| 158 | enum_member._value_ = value |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 159 | else: |
| 160 | enum_member = __new__(enum_class, *args) |
Ethan Furman | b41803e | 2013-07-25 13:50:45 -0700 | [diff] [blame] | 161 | if not hasattr(enum_member, '_value_'): |
| 162 | enum_member._value_ = member_type(*args) |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 163 | value = enum_member._value_ |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 164 | enum_member._name_ = member_name |
Ethan Furman | c850f34 | 2013-09-15 16:59:35 -0700 | [diff] [blame] | 165 | enum_member.__objclass__ = enum_class |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 166 | enum_member.__init__(*args) |
| 167 | # If another member with the same value was already defined, the |
| 168 | # new member becomes an alias to the existing one. |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 169 | for name, canonical_member in enum_class._member_map_.items(): |
Ethan Furman | 0081f23 | 2014-09-16 17:31:23 -0700 | [diff] [blame] | 170 | if canonical_member._value_ == enum_member._value_: |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 171 | enum_member = canonical_member |
| 172 | break |
| 173 | else: |
| 174 | # Aliases don't appear in member names (only in __members__). |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 175 | enum_class._member_names_.append(member_name) |
Ethan Furman | 354ecf1 | 2015-03-11 08:43:12 -0700 | [diff] [blame] | 176 | # performance boost for any member that would not shadow |
| 177 | # a DynamicClassAttribute |
| 178 | if member_name not in base_attributes: |
| 179 | setattr(enum_class, member_name, enum_member) |
| 180 | # now add to _member_map_ |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 181 | enum_class._member_map_[member_name] = enum_member |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 182 | try: |
| 183 | # This may fail if value is not hashable. We can't add the value |
| 184 | # to the map, and by-value lookups for this value will be |
| 185 | # linear. |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 186 | enum_class._value2member_map_[value] = enum_member |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 187 | except TypeError: |
| 188 | pass |
| 189 | |
| 190 | # double check that repr and friends are not the mixin's or various |
| 191 | # things break (such as pickle) |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 192 | for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 193 | class_method = getattr(enum_class, name) |
| 194 | obj_method = getattr(member_type, name, None) |
| 195 | enum_method = getattr(first_enum, name, None) |
| 196 | if obj_method is not None and obj_method is class_method: |
| 197 | setattr(enum_class, name, enum_method) |
| 198 | |
| 199 | # replace any other __new__ with our own (as long as Enum is not None, |
| 200 | # anyway) -- again, this is to support pickle |
| 201 | if Enum is not None: |
| 202 | # if the user defined their own __new__, save it before it gets |
| 203 | # clobbered in case they subclass later |
| 204 | if save_new: |
| 205 | enum_class.__new_member__ = __new__ |
| 206 | enum_class.__new__ = Enum.__new__ |
| 207 | return enum_class |
| 208 | |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 209 | def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 210 | """Either returns an existing member, or creates a new enum class. |
| 211 | |
| 212 | This method is used both when an enum class is given a value to match |
| 213 | to an enumeration member (i.e. Color(3)) and for the functional API |
| 214 | (i.e. Color = Enum('Color', names='red green blue')). |
| 215 | |
Ethan Furman | 2da9504 | 2014-03-03 12:42:52 -0800 | [diff] [blame] | 216 | When used for the functional API: |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 217 | |
Ethan Furman | 2da9504 | 2014-03-03 12:42:52 -0800 | [diff] [blame] | 218 | `value` will be the name of the new class. |
| 219 | |
| 220 | `names` should be either a string of white-space/comma delimited names |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 221 | (values will start at `start`), or an iterator/mapping of name, value pairs. |
Ethan Furman | 2da9504 | 2014-03-03 12:42:52 -0800 | [diff] [blame] | 222 | |
| 223 | `module` should be set to the module this class is being created in; |
| 224 | if it is not set, an attempt to find that module will be made, but if |
| 225 | it fails the class will not be picklable. |
| 226 | |
| 227 | `qualname` should be set to the actual location this class can be found |
| 228 | at in its module; by default it is set to the global scope. If this is |
| 229 | not correct, unpickling will fail in some circumstances. |
| 230 | |
| 231 | `type`, if set, will be mixed in as the first base class. |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 232 | |
| 233 | """ |
| 234 | if names is None: # simple value lookup |
| 235 | return cls.__new__(cls, value) |
| 236 | # otherwise, functional API: we're creating a new Enum type |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 237 | return cls._create_(value, names, module=module, qualname=qualname, type=type, start=start) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 238 | |
| 239 | def __contains__(cls, member): |
Ethan Furman | 0081f23 | 2014-09-16 17:31:23 -0700 | [diff] [blame] | 240 | return isinstance(member, cls) and member._name_ in cls._member_map_ |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 241 | |
Ethan Furman | 64a9972 | 2013-09-22 16:18:19 -0700 | [diff] [blame] | 242 | def __delattr__(cls, attr): |
| 243 | # nicer error message when someone tries to delete an attribute |
| 244 | # (see issue19025). |
| 245 | if attr in cls._member_map_: |
| 246 | raise AttributeError( |
| 247 | "%s: cannot delete Enum member." % cls.__name__) |
| 248 | super().__delattr__(attr) |
| 249 | |
Ethan Furman | 388a392 | 2013-08-12 06:51:41 -0700 | [diff] [blame] | 250 | def __dir__(self): |
Ethan Furman | 64a9972 | 2013-09-22 16:18:19 -0700 | [diff] [blame] | 251 | return (['__class__', '__doc__', '__members__', '__module__'] + |
| 252 | self._member_names_) |
Ethan Furman | 388a392 | 2013-08-12 06:51:41 -0700 | [diff] [blame] | 253 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 254 | def __getattr__(cls, name): |
| 255 | """Return the enum member matching `name` |
| 256 | |
| 257 | We use __getattr__ instead of descriptors or inserting into the enum |
| 258 | class' __dict__ in order to support `name` and `value` being both |
| 259 | properties for enum members (which live in the class' __dict__) and |
| 260 | enum members themselves. |
| 261 | |
| 262 | """ |
| 263 | if _is_dunder(name): |
| 264 | raise AttributeError(name) |
| 265 | try: |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 266 | return cls._member_map_[name] |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 267 | except KeyError: |
| 268 | raise AttributeError(name) from None |
| 269 | |
| 270 | def __getitem__(cls, name): |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 271 | return cls._member_map_[name] |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 272 | |
| 273 | def __iter__(cls): |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 274 | return (cls._member_map_[name] for name in cls._member_names_) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 275 | |
| 276 | def __len__(cls): |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 277 | return len(cls._member_names_) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 278 | |
Ethan Furman | 2131a4a | 2013-09-14 18:11:24 -0700 | [diff] [blame] | 279 | @property |
| 280 | def __members__(cls): |
| 281 | """Returns a mapping of member name->value. |
| 282 | |
| 283 | This mapping lists all enum members, including aliases. Note that this |
| 284 | is a read-only view of the internal mapping. |
| 285 | |
| 286 | """ |
| 287 | return MappingProxyType(cls._member_map_) |
| 288 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 289 | def __repr__(cls): |
| 290 | return "<enum %r>" % cls.__name__ |
| 291 | |
Ethan Furman | 2131a4a | 2013-09-14 18:11:24 -0700 | [diff] [blame] | 292 | def __reversed__(cls): |
| 293 | return (cls._member_map_[name] for name in reversed(cls._member_names_)) |
| 294 | |
Ethan Furman | f203f2d | 2013-09-06 07:16:48 -0700 | [diff] [blame] | 295 | def __setattr__(cls, name, value): |
| 296 | """Block attempts to reassign Enum members. |
| 297 | |
| 298 | A simple assignment to the class namespace only changes one of the |
| 299 | several possible ways to get an Enum member from the Enum class, |
| 300 | resulting in an inconsistent Enumeration. |
| 301 | |
| 302 | """ |
| 303 | member_map = cls.__dict__.get('_member_map_', {}) |
| 304 | if name in member_map: |
| 305 | raise AttributeError('Cannot reassign members.') |
| 306 | super().__setattr__(name, value) |
| 307 | |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 308 | def _create_(cls, class_name, names=None, *, module=None, qualname=None, type=None, start=1): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 309 | """Convenience method to create a new Enum class. |
| 310 | |
| 311 | `names` can be: |
| 312 | |
| 313 | * A string containing member names, separated either with spaces or |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 314 | commas. Values are incremented by 1 from `start`. |
| 315 | * An iterable of member names. Values are incremented by 1 from `start`. |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 316 | * An iterable of (member name, value) pairs. |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 317 | * A mapping of member name -> value pairs. |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 318 | |
| 319 | """ |
| 320 | metacls = cls.__class__ |
| 321 | bases = (cls, ) if type is None else (type, cls) |
| 322 | classdict = metacls.__prepare__(class_name, bases) |
| 323 | |
| 324 | # special processing needed for names? |
| 325 | if isinstance(names, str): |
| 326 | names = names.replace(',', ' ').split() |
| 327 | if isinstance(names, (tuple, list)) and isinstance(names[0], str): |
Ethan Furman | d9925a1 | 2014-09-16 20:35:55 -0700 | [diff] [blame] | 328 | names = [(e, i) for (i, e) in enumerate(names, start)] |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 329 | |
| 330 | # Here, names is either an iterable of (name, value) or a mapping. |
| 331 | for item in names: |
| 332 | if isinstance(item, str): |
| 333 | member_name, member_value = item, names[item] |
| 334 | else: |
| 335 | member_name, member_value = item |
| 336 | classdict[member_name] = member_value |
| 337 | enum_class = metacls.__new__(metacls, class_name, bases, classdict) |
| 338 | |
| 339 | # TODO: replace the frame hack if a blessed way to know the calling |
| 340 | # module is ever developed |
| 341 | if module is None: |
| 342 | try: |
| 343 | module = sys._getframe(2).f_globals['__name__'] |
| 344 | except (AttributeError, ValueError) as exc: |
| 345 | pass |
| 346 | if module is None: |
| 347 | _make_class_unpicklable(enum_class) |
| 348 | else: |
| 349 | enum_class.__module__ = module |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 350 | if qualname is not None: |
| 351 | enum_class.__qualname__ = qualname |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 352 | |
| 353 | return enum_class |
| 354 | |
| 355 | @staticmethod |
| 356 | def _get_mixins_(bases): |
| 357 | """Returns the type for creating enum members, and the first inherited |
| 358 | enum class. |
| 359 | |
| 360 | bases: the tuple of bases that was given to __new__ |
| 361 | |
| 362 | """ |
| 363 | if not bases: |
| 364 | return object, Enum |
| 365 | |
| 366 | # double check that we are not subclassing a class with existing |
| 367 | # enumeration members; while we're at it, see if any other data |
| 368 | # type has been mixed in so we can use the correct __new__ |
| 369 | member_type = first_enum = None |
| 370 | for base in bases: |
| 371 | if (base is not Enum and |
| 372 | issubclass(base, Enum) and |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 373 | base._member_names_): |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 374 | raise TypeError("Cannot extend enumerations") |
| 375 | # base is now the last base in bases |
| 376 | if not issubclass(base, Enum): |
| 377 | raise TypeError("new enumerations must be created as " |
| 378 | "`ClassName([mixin_type,] enum_type)`") |
| 379 | |
| 380 | # get correct mix-in type (either mix-in type of Enum subclass, or |
| 381 | # first base if last base is Enum) |
| 382 | if not issubclass(bases[0], Enum): |
| 383 | member_type = bases[0] # first data type |
| 384 | first_enum = bases[-1] # enum type |
| 385 | else: |
| 386 | for base in bases[0].__mro__: |
| 387 | # most common: (IntEnum, int, Enum, object) |
| 388 | # possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>, |
| 389 | # <class 'int'>, <Enum 'Enum'>, |
| 390 | # <class 'object'>) |
| 391 | if issubclass(base, Enum): |
| 392 | if first_enum is None: |
| 393 | first_enum = base |
| 394 | else: |
| 395 | if member_type is None: |
| 396 | member_type = base |
| 397 | |
| 398 | return member_type, first_enum |
| 399 | |
| 400 | @staticmethod |
| 401 | def _find_new_(classdict, member_type, first_enum): |
| 402 | """Returns the __new__ to be used for creating the enum members. |
| 403 | |
| 404 | classdict: the class dictionary given to __new__ |
| 405 | member_type: the data type whose __new__ will be used by default |
| 406 | first_enum: enumeration to check for an overriding __new__ |
| 407 | |
| 408 | """ |
| 409 | # now find the correct __new__, checking to see of one was defined |
| 410 | # by the user; also check earlier enum classes in case a __new__ was |
| 411 | # saved as __new_member__ |
| 412 | __new__ = classdict.get('__new__', None) |
| 413 | |
| 414 | # should __new__ be saved as __new_member__ later? |
| 415 | save_new = __new__ is not None |
| 416 | |
| 417 | if __new__ is None: |
| 418 | # check all possibles for __new_member__ before falling back to |
| 419 | # __new__ |
| 420 | for method in ('__new_member__', '__new__'): |
| 421 | for possible in (member_type, first_enum): |
| 422 | target = getattr(possible, method, None) |
| 423 | if target not in { |
| 424 | None, |
| 425 | None.__new__, |
| 426 | object.__new__, |
| 427 | Enum.__new__, |
| 428 | }: |
| 429 | __new__ = target |
| 430 | break |
| 431 | if __new__ is not None: |
| 432 | break |
| 433 | else: |
| 434 | __new__ = object.__new__ |
| 435 | |
| 436 | # if a non-object.__new__ is used then whatever value/tuple was |
| 437 | # assigned to the enum member name will be passed to __new__ and to the |
| 438 | # new enum member's __init__ |
| 439 | if __new__ is object.__new__: |
| 440 | use_args = False |
| 441 | else: |
| 442 | use_args = True |
| 443 | |
| 444 | return __new__, save_new, use_args |
| 445 | |
| 446 | |
| 447 | class Enum(metaclass=EnumMeta): |
| 448 | """Generic enumeration. |
| 449 | |
| 450 | Derive from this class to define new enumerations. |
| 451 | |
| 452 | """ |
| 453 | def __new__(cls, value): |
| 454 | # all enum instances are actually created during class construction |
| 455 | # without calling this method; this method is called by the metaclass' |
| 456 | # __call__ (i.e. Color(3) ), and by pickle |
| 457 | if type(value) is cls: |
| 458 | # For lookups like Color(Color.red) |
| 459 | return value |
| 460 | # by-value search for a matching enum member |
| 461 | # see if it's in the reverse mapping (for hashable values) |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 462 | try: |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 463 | if value in cls._value2member_map_: |
| 464 | return cls._value2member_map_[value] |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 465 | except TypeError: |
| 466 | # not there, now do long search -- O(n) behavior |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 467 | for member in cls._member_map_.values(): |
Ethan Furman | 0081f23 | 2014-09-16 17:31:23 -0700 | [diff] [blame] | 468 | if member._value_ == value: |
Ethan Furman | 2aa2732 | 2013-07-19 19:35:56 -0700 | [diff] [blame] | 469 | return member |
Ethan Furman | 0081f23 | 2014-09-16 17:31:23 -0700 | [diff] [blame] | 470 | raise ValueError("%r is not a valid %s" % (value, cls.__name__)) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 471 | |
| 472 | def __repr__(self): |
| 473 | return "<%s.%s: %r>" % ( |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 474 | self.__class__.__name__, self._name_, self._value_) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 475 | |
| 476 | def __str__(self): |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 477 | return "%s.%s" % (self.__class__.__name__, self._name_) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 478 | |
Ethan Furman | 388a392 | 2013-08-12 06:51:41 -0700 | [diff] [blame] | 479 | def __dir__(self): |
Ethan Furman | 0ae550b | 2014-10-14 08:58:32 -0700 | [diff] [blame] | 480 | added_behavior = [ |
| 481 | m |
| 482 | for cls in self.__class__.mro() |
| 483 | for m in cls.__dict__ |
Ethan Furman | 354ecf1 | 2015-03-11 08:43:12 -0700 | [diff] [blame] | 484 | if m[0] != '_' and m not in self._member_map_ |
Ethan Furman | 0ae550b | 2014-10-14 08:58:32 -0700 | [diff] [blame] | 485 | ] |
Ethan Furman | ec5f8eb | 2014-10-21 13:40:35 -0700 | [diff] [blame] | 486 | return (['__class__', '__doc__', '__module__'] + added_behavior) |
Ethan Furman | 388a392 | 2013-08-12 06:51:41 -0700 | [diff] [blame] | 487 | |
Ethan Furman | ec15a82 | 2013-08-31 19:17:41 -0700 | [diff] [blame] | 488 | def __format__(self, format_spec): |
| 489 | # mixed-in Enums should use the mixed-in type's __format__, otherwise |
| 490 | # we can get strange results with the Enum name showing up instead of |
| 491 | # the value |
| 492 | |
| 493 | # pure Enum branch |
| 494 | if self._member_type_ is object: |
| 495 | cls = str |
| 496 | val = str(self) |
| 497 | # mix-in branch |
| 498 | else: |
| 499 | cls = self._member_type_ |
Ethan Furman | 0081f23 | 2014-09-16 17:31:23 -0700 | [diff] [blame] | 500 | val = self._value_ |
Ethan Furman | ec15a82 | 2013-08-31 19:17:41 -0700 | [diff] [blame] | 501 | return cls.__format__(val, format_spec) |
| 502 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 503 | def __hash__(self): |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 504 | return hash(self._name_) |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 505 | |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 506 | def __reduce_ex__(self, proto): |
Ethan Furman | dc87052 | 2014-02-18 12:37:12 -0800 | [diff] [blame] | 507 | return self.__class__, (self._value_, ) |
Ethan Furman | ca1b794 | 2014-02-08 11:36:27 -0800 | [diff] [blame] | 508 | |
Ethan Furman | 33918c1 | 2013-09-27 23:02:02 -0700 | [diff] [blame] | 509 | # DynamicClassAttribute is used to provide access to the `name` and |
| 510 | # `value` properties of enum members while keeping some measure of |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 511 | # protection from modification, while still allowing for an enumeration |
| 512 | # to have members named `name` and `value`. This works because enumeration |
| 513 | # members are not set directly on the enum class -- __getattr__ is |
| 514 | # used to look them up. |
| 515 | |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 516 | @DynamicClassAttribute |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 517 | def name(self): |
Ethan Furman | c850f34 | 2013-09-15 16:59:35 -0700 | [diff] [blame] | 518 | """The name of the Enum member.""" |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 519 | return self._name_ |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 520 | |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 521 | @DynamicClassAttribute |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 522 | def value(self): |
Ethan Furman | c850f34 | 2013-09-15 16:59:35 -0700 | [diff] [blame] | 523 | """The value of the Enum member.""" |
Ethan Furman | 520ad57 | 2013-07-19 19:47:21 -0700 | [diff] [blame] | 524 | return self._value_ |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 525 | |
Ethan Furman | 24e837f | 2015-03-18 17:27:57 -0700 | [diff] [blame] | 526 | @classmethod |
| 527 | def _convert(cls, name, module, filter, source=None): |
| 528 | """ |
| 529 | Create a new Enum subclass that replaces a collection of global constants |
| 530 | """ |
| 531 | # convert all constants from source (or module) that pass filter() to |
| 532 | # a new Enum called name, and export the enum and its members back to |
| 533 | # module; |
| 534 | # also, replace the __reduce_ex__ method so unpickling works in |
| 535 | # previous Python versions |
| 536 | module_globals = vars(sys.modules[module]) |
| 537 | if source: |
| 538 | source = vars(source) |
| 539 | else: |
| 540 | source = module_globals |
| 541 | members = {name: value for name, value in source.items() |
| 542 | if filter(name)} |
| 543 | cls = cls(name, members, module=module) |
| 544 | cls.__reduce_ex__ = _reduce_ex_by_name |
| 545 | module_globals.update(cls.__members__) |
| 546 | module_globals[name] = cls |
| 547 | return cls |
| 548 | |
Ethan Furman | 6b3d64a | 2013-06-14 16:55:46 -0700 | [diff] [blame] | 549 | |
| 550 | class IntEnum(int, Enum): |
| 551 | """Enum where members are also (and must be) ints""" |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 552 | |
| 553 | |
Ethan Furman | 24e837f | 2015-03-18 17:27:57 -0700 | [diff] [blame] | 554 | def _reduce_ex_by_name(self, proto): |
| 555 | return self.name |
| 556 | |
Ethan Furman | f24bb35 | 2013-07-18 17:05:39 -0700 | [diff] [blame] | 557 | def unique(enumeration): |
| 558 | """Class decorator for enumerations ensuring unique member values.""" |
| 559 | duplicates = [] |
| 560 | for name, member in enumeration.__members__.items(): |
| 561 | if name != member.name: |
| 562 | duplicates.append((name, member.name)) |
| 563 | if duplicates: |
| 564 | alias_details = ', '.join( |
| 565 | ["%s -> %s" % (alias, name) for (alias, name) in duplicates]) |
| 566 | raise ValueError('duplicate values found in %r: %s' % |
| 567 | (enumeration, alias_details)) |
| 568 | return enumeration |