cpython: e7a01c7f69fe (original) (raw)
new file mode 100644
--- /dev/null
+++ b/Doc/library/enum.rst
@@ -0,0 +1,542 @@
+:mod:enum
--- Support for enumerations
+========================================
+
+.. module:: enum
+.. :synopsis: enumerations are sets of symbolic names bound to unique, constant
- values.
+.. :moduleauthor:: Ethan Furman ethan@stoneleaf.us
+.. :sectionauthor:: Barry Warsaw barry@python.org,
+.. :sectionauthor:: Eli Bendersky eliben@gmail.com,
+.. :sectionauthor:: Ethan Furman ethan@stoneleaf.us
+
+Source code: :source:
Lib/enum.py
+ +---------------- + +An enumeration is a set of symbolic names (members) bound to unique, constant +values. Within an enumeration, the members can be compared by identity, and +the enumeration itself can be iterated over. + +This module defines two enumeration classes that can be used to define unique +sets of names and values: :class:Enum
and :class:IntEnum
. + +Creating an Enum +---------------- + +Enumerations are created using the :keyword:class
syntax, which makes them +easy to read and write. An alternative creation method is described in +Functional API
_. To define an enumeration, subclass :class:Enum
as +follows:: + - ... red = 1
- ... green = 2
- ... blue = 3
+
+A note on nomenclature: we call :class:Color
an enumeration (or enum)
+and :attr:Color.red
, :attr:Color.green
are enumeration members (or
+enum members). Enumeration members also have values (the value of
+:attr:Color.red
is 1
, etc.)
+
+Enumeration members have human readable string representations::
+
+
+...while their repr
has more information::
+
+ +The type of an enumeration member is the enumeration it belongs to:: +
+ +Enum members also have a property that contains just their item name:: +
+ +Enumerations support iteration, in definition order:: +
- ... vanilla = 7
- ... chocolate = 4
- ... cookies = 9
- ... mint = 3
- ...
- ... print(shake)
- ...
- Shake.vanilla
- Shake.chocolate
- Shake.cookies
- Shake.mint
+ +Enumeration members are hashable, so they can be used in dictionaries and sets:: +
+
+
+Programmatic access to enumeration members
+------------------------------------------
+
+Sometimes it's useful to access members in enumerations programmatically (i.e.
+situations where Color.red
won't do because the exact color is not known
+at program-writing time). Enum
allows such access::
+
+ +If you want to access enum members by name, use item access:: +
+ + +Duplicating enum members and values +----------------------------------- + +Having two enum members with the same name is invalid:: +
- ... square = 2
- ... square = 3
- ...
- Traceback (most recent call last):
- ...
- TypeError: Attempted to reuse key: 'square'
+ +However, two enum members are allowed to have the same value. Given two members +A and B with the same value (and A defined first), B is an alias to A. By-value +lookup of the value of A and B will return A. By-name lookup of B will also +return A:: +
- ... square = 2
- ... diamond = 1
- ... circle = 3
- ... alias_for_square = 2
- ...
- <Shape.square: 2>
- <Shape.square: 2>
- <Shape.square: 2>
+ +Iterating over the members of an enum does not provide the aliases:: +
+
+The special attribute __members__
is an ordered dictionary mapping names
+to members. It includes all names defined in the enumeration, including the
+aliases::
+
- ... name, member
- ...
- ('square', <Shape.square: 2>)
- ('diamond', <Shape.diamond: 1>)
- ('circle', <Shape.circle: 3>)
- ('alias_for_square', <Shape.square: 2>)
+
+The __members__
attribute can be used for detailed programmatic access to
+the enumeration members. For example, finding all the aliases::
+
+ +Comparisons +----------- + +Enumeration members are compared by identity:: +
+
+Ordered comparisons between enumeration values are not supported. Enum
+members are not integers (but see IntEnum
_ below)::
+
- Traceback (most recent call last):
File "<stdin>", line 1, in <module>[](#l1.185)
- TypeError: unorderable types: Color() < Color()
+ +Equality comparisons are defined though:: +
+
+Comparisons against non-enumeration values will always compare not equal
+(again, class:IntEnum
was explicitly designed to behave differently, see
+below)::
+
+
+
+Allowed members and attributes of enumerations
+----------------------------------------------
+
+The examples above use integers for enumeration values. Using integers is
+short and handy (and provided by default by the Functional API
_), but not
+strictly enforced. In the vast majority of use-cases, one doesn't care what
+the actual value of an enumeration is. But if the value is important,
+enumerations can have arbitrary values.
+
+Enumerations are Python classes, and can have methods and special methods as
+usual. If we have this enumeration::
+
- ... funky = 1
- ... happy = 3
- ...
- ... def describe(self):
- ... # self is the member here
- ... return self.name, self.value
- ...
- ... def str(self):
- ... return 'my custom str! {0}'.format(self.value)
- ...
- ... @classmethod
- ... def favorite_mood(cls):
- ... # cls here is the enumeration
- ... return cls.happy
+
+The rules for what is allowed are as follows: sunder names (starting and
+ending with a single underscore) are reserved by enum and cannot be used;
+all other attributes defined within an enumeration will become members of this
+enumeration, with the exception of dunder names and descriptors (methods
+are also descriptors).
+
+Note: if your enumeration defines :meth:__new__
and/or :meth:__init__
then
+whatever value(s) were given to the enum member will be passed into those
+methods. See Planet
_ for an example.
+
+
+Restricted subclassing of enumerations
+--------------------------------------
+
+Subclassing an enumeration is allowed only if the enumeration does not define
+any members. So this is forbidden::
+
+
+Allowing subclassing of enums that define members would lead to a violation of
+some important invariants of types and instances. On the other hand, it makes
+sense to allow sharing some common behavior between a group of enumerations.
+(See OrderedEnum
_ for an example.)
+
+
+Pickling
+--------
+
+Enumerations can be pickled and unpickled::
+
+ +The usual restrictions for pickling apply: picklable enums must be defined in +the top level of a module, since unpickling requires them to be importable +from that module. + +.. warning:: +
- In order to support the singleton nature of enumeration members, pickle
- protocol version 2 or higher must be used.
+
+
+Functional API
+--------------
+
+The :class:Enum
class is callable, providing the following functional API::
+
- <enum 'Animal'>
- <Animal.ant: 1>
- 1
- [<Animal.ant: 1>, <Animal.bee: 2>, <Animal.cat: 3>, <Animal.dog: 4>]
+
+The semantics of this API resemble :class:namedtuple
. The first argument
+of the call to :class:Enum
is the name of the enumeration.
+
+The second argument is the source of enumeration member names. It can be a
+whitespace-separated string of names, a sequence of names, a sequence of
+2-tuples with key/value pairs, or a mapping (e.g. dictionary) of names to
+values. The last two options enable assigning arbitrary values to
+enumerations; the others auto-assign increasing integers starting with 1. A
+new class derived from :class:Enum
is returned. In other words, the above
+assignment to :class:Animal
is equivalent to::
+
+ +Pickling enums created with the functional API can be tricky as frame stack +implementation details are used to try and figure out which module the +enumeration is being created in (e.g. it will fail if you use a utility +function in separate module, and also may not work on IronPython or Jython). +The solution is to specify the module name explicitly as follows:: +
+
+Derived Enumerations
+====================
+
+IntEnum
+-------
+
+A variation of :class:Enum
is provided which is also a subclass of
+:class:int
. Members of an :class:IntEnum
can be compared to integers;
+by extension, integer enumerations of different types can also be compared
+to each other::
+
+
+However, they still can't be compared to standard :class:Enum
enumerations::
+
+
+:class:IntEnum
values behave like integers in other ways you'd expect::
+
+
+For the vast majority of code, :class:Enum
is strongly recommended,
+since :class:IntEnum
breaks some semantic promises of an enumeration (by
+being comparable to integers, and thus by transitivity to other
+unrelated enumerations). It should be used only in special cases where
+there's no other choice; for example, when integer constants are
+replaced with enumerations and backwards compatibility is required with code
+that still expects integers.
+
+
+Others
+------
+
+While :class:IntEnum
is part of the :mod:enum
module, it would be very
+simple to implement independently::
+
+
+This demonstrates how similar derived enumerations can be defined; for example
+a :class:StrEnum
that mixes in :class:str
instead of :class:int
.
+
+Some rules:
+
+1. When subclassing :class:Enum
, mix-in types must appear before
- :class:
Enum
itself in the sequence of bases, as in the :class:IntEnum
- example above.
+2. While :class:
Enum
can have members of any type, once you mix in an - additional type, all the members must have values of that type, e.g.
- :class:
int
above. This restriction does not apply to mix-ins which only - add methods and don't specify another data type such as :class:
int
or - :class:
str
. +3. When another data type is mixed in, the :attr:value
attribute is *not the - same* as the enum member itself, although it is equivalant and will compare
- equal. +
+
+Interesting examples
+====================
+
+While :class:Enum
and :class:IntEnum
are expected to cover the majority of
+use-cases, they cannot cover them all. Here are recipes for some different
+types of enumerations that can be used directly, or as examples for creating
+one's own.
+
+
+AutoNumber
+----------
+
+Avoids having to specify the value for each enumeration member::
+
- ... def new(cls):
- ... value = len(cls.members) + 1
- ... obj = object.new(cls)
- ... obj._value = value
- ... return obj
- ...
- ... red = ()
- ... green = ()
- ... blue = ()
- ...
- True
+ + +UniqueEnum +---------- + +Raises an error if a duplicate member name is found instead of creating an +alias:: +
- ... def init(self, *args):
- ... cls = self.class
- ... if any(self.value == e.value for e in cls):
- ... a = self.name
- ... e = cls(self.value).name
- ... raise ValueError(
- ... "aliases not allowed in UniqueEnum: %r --> %r"
- ... % (a, e))
- ...
- ... red = 1
- ... green = 2
- ... blue = 3
- ... grene = 2
- Traceback (most recent call last):
- ...
- ValueError: aliases not allowed in UniqueEnum: 'grene' --> 'green'
+
+
+OrderedEnum
+-----------
+
+An ordered enumeration that is not based on :class:IntEnum
and so maintains
+the normal :class:Enum
invariants (such as not being comparable to other
+enumerations)::
+
- ... def ge(self, other):
- ... if self.class is other.class:
- ... return self._value >= other._value
- ... return NotImplemented
- ... def gt(self, other):
- ... if self.class is other.class:
- ... return self._value > other._value
- ... return NotImplemented
- ... def le(self, other):
- ... if self.class is other.class:
- ... return self._value <= other._value
- ... return NotImplemented
- ... def lt(self, other):
- ... if self.class is other.class:
- ... return self._value < other._value
- ... return NotImplemented
- ...
- ... A = 5
- ... B = 4
- ... C = 3
- ... D = 2
- ... F = 1
- ...
- True
+
+
+Planet
+------
+
+If :meth:__new__
or :meth:__init__
is defined the value of the enum member
+will be passed to those methods::
+
- ... MERCURY = (3.303e+23, 2.4397e6)
- ... VENUS = (4.869e+24, 6.0518e6)
- ... EARTH = (5.976e+24, 6.37814e6)
- ... MARS = (6.421e+23, 3.3972e6)
- ... JUPITER = (1.9e+27, 7.1492e7)
- ... SATURN = (5.688e+26, 6.0268e7)
- ... URANUS = (8.686e+25, 2.5559e7)
- ... NEPTUNE = (1.024e+26, 2.4746e7)
- ... def init(self, mass, radius):
- ... self.mass = mass # in kilograms
- ... self.radius = radius # in meters
- ... @property
- ... def surface_gravity(self):
- ... # universal gravitational constant (m3 kg-1 s-2)
- ... G = 6.67300E-11
- ... return G * self.mass / (self.radius * self.radius)
- ...
- (5.976e+24, 6378140.0)
- 9.802652743337129
new file mode 100644 --- /dev/null +++ b/Lib/enum.py @@ -0,0 +1,465 @@ +"""Python Enumerations""" + +import sys +from collections import OrderedDict +from types import MappingProxyType + +all = ['Enum', 'IntEnum'] + + +class _RouteClassAttributeToGetattr:
- This is a descriptor, used to define attributes that act differently when
- accessed through an instance and through a class. Instance access remains
- normal, but access to an attribute through a class will be routed to the
- class's getattr method; this is done by raising AttributeError.
- def get(self, instance, ownerclass=None):
if instance is None:[](#l2.27)
raise AttributeError()[](#l2.28)
return self.fget(instance)[](#l2.29)
- """Returns True if a dunder name, False otherwise."""
- return (name[:2] == name[-2:] == '__' and
name[2:3] != '_' and[](#l2.41)
name[-3:-2] != '_')[](#l2.42)
- """Returns True if a sunder name, False otherwise."""
- return (name[0] == name[-1] == '_' and
name[1:2] != '_' and[](#l2.48)
name[-2:-1] != '_')[](#l2.49)
+ + +def _make_class_unpicklable(cls):
- """Make the given class un-picklable."""
- def _break_on_call_reduce(self):
raise TypeError('%r cannot be pickled' % self)[](#l2.55)
- cls.reduce = _break_on_call_reduce
- cls.module = ''
- def setitem(self, key, value):
"""Changes anything not dundered or that doesn't have __get__.[](#l2.72)
If a descriptor is added with the same name as an enum member, the name[](#l2.74)
is removed from _member_names (this may leave a hole in the numerical[](#l2.75)
sequence of values).[](#l2.76)
If an enum member name is used twice, an error is raised; duplicate[](#l2.78)
values are not checked for.[](#l2.79)
Single underscore (sunder) names are reserved.[](#l2.81)
"""[](#l2.83)
if _is_sunder(key):[](#l2.84)
raise ValueError('_names_ are reserved for future Enum use')[](#l2.85)
elif _is_dunder(key) or hasattr(value, '__get__'):[](#l2.86)
if key in self._member_names:[](#l2.87)
# overwriting an enum with a method? then remove the name from[](#l2.88)
# _member_names or it will become an enum anyway when the class[](#l2.89)
# is created[](#l2.90)
self._member_names.remove(key)[](#l2.91)
else:[](#l2.92)
if key in self._member_names:[](#l2.93)
raise TypeError('Attempted to reuse key: %r' % key)[](#l2.94)
self._member_names.append(key)[](#l2.95)
super().__setitem__(key, value)[](#l2.96)
+
+
+# Dummy value for Enum as EnumMeta explicity checks for it, but of course until
+# EnumMeta finishes running the first time the Enum class doesn't exist. This
+# is also why there are checks in EnumMeta like if Enum is not None
+Enum = None
+
+
+class EnumMeta(type):
- """Metaclass for Enum"""
- @classmethod
- def prepare(metacls, cls, bases):
return _EnumDict()[](#l2.109)
- def new(metacls, cls, bases, classdict):
# an Enum class is final once enumeration items have been defined; it[](#l2.112)
# cannot be mixed with other types (int, float, etc.) if it has an[](#l2.113)
# inherited __new__ unless a new __new__ is defined (or the resulting[](#l2.114)
# class will fail).[](#l2.115)
member_type, first_enum = metacls._get_mixins_(bases)[](#l2.116)
__new__, save_new, use_args = metacls._find_new_(classdict, member_type,[](#l2.117)
first_enum)[](#l2.118)
# save enum items into separate mapping so they don't get baked into[](#l2.120)
# the new class[](#l2.121)
members = {k: classdict[k] for k in classdict._member_names}[](#l2.122)
for name in classdict._member_names:[](#l2.123)
del classdict[name][](#l2.124)
# check for illegal enum names (any others?)[](#l2.126)
invalid_names = set(members) & {'mro', }[](#l2.127)
if invalid_names:[](#l2.128)
raise ValueError('Invalid enum member name: {0}'.format([](#l2.129)
','.join(invalid_names)))[](#l2.130)
# create our new Enum type[](#l2.132)
enum_class = super().__new__(metacls, cls, bases, classdict)[](#l2.133)
enum_class._member_names = [] # names in definition order[](#l2.134)
enum_class._member_map = OrderedDict() # name->value map[](#l2.135)
# Reverse value->name map for hashable values.[](#l2.137)
enum_class._value2member_map = {}[](#l2.138)
# check for a __getnewargs__, and if not present sabotage[](#l2.140)
# pickling, since it won't work anyway[](#l2.141)
if (member_type is not object and[](#l2.142)
member_type.__dict__.get('__getnewargs__') is None[](#l2.143)
):[](#l2.144)
_make_class_unpicklable(enum_class)[](#l2.145)
# instantiate them, checking for duplicates as we go[](#l2.147)
# we instantiate first instead of checking for duplicates first in case[](#l2.148)
# a custom __new__ is doing something funky with the values -- such as[](#l2.149)
# auto-numbering ;)[](#l2.150)
for member_name in classdict._member_names:[](#l2.151)
value = members[member_name][](#l2.152)
if not isinstance(value, tuple):[](#l2.153)
args = (value, )[](#l2.154)
else:[](#l2.155)
args = value[](#l2.156)
if member_type is tuple: # special case for tuple enums[](#l2.157)
args = (args, ) # wrap it one more time[](#l2.158)
if not use_args:[](#l2.159)
enum_member = __new__(enum_class)[](#l2.160)
enum_member._value = value[](#l2.161)
else:[](#l2.162)
enum_member = __new__(enum_class, *args)[](#l2.163)
if not hasattr(enum_member, '_value'):[](#l2.164)
enum_member._value = member_type(*args)[](#l2.165)
enum_member._member_type = member_type[](#l2.166)
enum_member._name = member_name[](#l2.167)
enum_member.__init__(*args)[](#l2.168)
# If another member with the same value was already defined, the[](#l2.169)
# new member becomes an alias to the existing one.[](#l2.170)
for name, canonical_member in enum_class._member_map.items():[](#l2.171)
if canonical_member.value == enum_member._value:[](#l2.172)
enum_member = canonical_member[](#l2.173)
break[](#l2.174)
else:[](#l2.175)
# Aliases don't appear in member names (only in __members__).[](#l2.176)
enum_class._member_names.append(member_name)[](#l2.177)
enum_class._member_map[member_name] = enum_member[](#l2.178)
try:[](#l2.179)
# This may fail if value is not hashable. We can't add the value[](#l2.180)
# to the map, and by-value lookups for this value will be[](#l2.181)
# linear.[](#l2.182)
enum_class._value2member_map[value] = enum_member[](#l2.183)
except TypeError:[](#l2.184)
pass[](#l2.185)
# double check that repr and friends are not the mixin's or various[](#l2.187)
# things break (such as pickle)[](#l2.188)
for name in ('__repr__', '__str__', '__getnewargs__'):[](#l2.189)
class_method = getattr(enum_class, name)[](#l2.190)
obj_method = getattr(member_type, name, None)[](#l2.191)
enum_method = getattr(first_enum, name, None)[](#l2.192)
if obj_method is not None and obj_method is class_method:[](#l2.193)
setattr(enum_class, name, enum_method)[](#l2.194)
# replace any other __new__ with our own (as long as Enum is not None,[](#l2.196)
# anyway) -- again, this is to support pickle[](#l2.197)
if Enum is not None:[](#l2.198)
# if the user defined their own __new__, save it before it gets[](#l2.199)
# clobbered in case they subclass later[](#l2.200)
if save_new:[](#l2.201)
enum_class.__new_member__ = __new__[](#l2.202)
enum_class.__new__ = Enum.__new__[](#l2.203)
return enum_class[](#l2.204)
- def call(cls, value, names=None, *, module=None, type=None):
"""Either returns an existing member, or creates a new enum class.[](#l2.207)
This method is used both when an enum class is given a value to match[](#l2.209)
to an enumeration member (i.e. Color(3)) and for the functional API[](#l2.210)
(i.e. Color = Enum('Color', names='red green blue')).[](#l2.211)
When used for the functional API: `module`, if set, will be stored in[](#l2.213)
the new class' __module__ attribute; `type`, if set, will be mixed in[](#l2.214)
as the first base class.[](#l2.215)
Note: if `module` is not set this routine will attempt to discover the[](#l2.217)
calling module by walking the frame stack; if this is unsuccessful[](#l2.218)
the resulting class will not be pickleable.[](#l2.219)
"""[](#l2.221)
if names is None: # simple value lookup[](#l2.222)
return cls.__new__(cls, value)[](#l2.223)
# otherwise, functional API: we're creating a new Enum type[](#l2.224)
return cls._create_(value, names, module=module, type=type)[](#l2.225)
- def contains(cls, member):
return isinstance(member, cls) and member.name in cls._member_map[](#l2.228)
This mapping lists all enum members, including aliases. Note that this[](#l2.237)
is a read-only view of the internal mapping.[](#l2.238)
"""[](#l2.240)
return MappingProxyType(cls._member_map)[](#l2.241)
We use __getattr__ instead of descriptors or inserting into the enum[](#l2.246)
class' __dict__ in order to support `name` and `value` being both[](#l2.247)
properties for enum members (which live in the class' __dict__) and[](#l2.248)
enum members themselves.[](#l2.249)
"""[](#l2.251)
if _is_dunder(name):[](#l2.252)
raise AttributeError(name)[](#l2.253)
try:[](#l2.254)
return cls._member_map[name][](#l2.255)
except KeyError:[](#l2.256)
raise AttributeError(name) from None[](#l2.257)
- def create(cls, class_name, names=None, *, module=None, type=None):
"""Convenience method to create a new Enum class.[](#l2.272)
`names` can be:[](#l2.274)
* A string containing member names, separated either with spaces or[](#l2.276)
commas. Values are auto-numbered from 1.[](#l2.277)
* An iterable of member names. Values are auto-numbered from 1.[](#l2.278)
* An iterable of (member name, value) pairs.[](#l2.279)
* A mapping of member name -> value.[](#l2.280)
"""[](#l2.282)
metacls = cls.__class__[](#l2.283)
bases = (cls, ) if type is None else (type, cls)[](#l2.284)
classdict = metacls.__prepare__(class_name, bases)[](#l2.285)
# special processing needed for names?[](#l2.287)
if isinstance(names, str):[](#l2.288)
names = names.replace(',', ' ').split()[](#l2.289)
if isinstance(names, (tuple, list)) and isinstance(names[0], str):[](#l2.290)
names = [(e, i) for (i, e) in enumerate(names, 1)][](#l2.291)
# Here, names is either an iterable of (name, value) or a mapping.[](#l2.293)
for item in names:[](#l2.294)
if isinstance(item, str):[](#l2.295)
member_name, member_value = item, names[item][](#l2.296)
else:[](#l2.297)
member_name, member_value = item[](#l2.298)
classdict[member_name] = member_value[](#l2.299)
enum_class = metacls.__new__(metacls, class_name, bases, classdict)[](#l2.300)
# TODO: replace the frame hack if a blessed way to know the calling[](#l2.302)
# module is ever developed[](#l2.303)
if module is None:[](#l2.304)
try:[](#l2.305)
module = sys._getframe(2).f_globals['__name__'][](#l2.306)
except (AttributeError, ValueError) as exc:[](#l2.307)
pass[](#l2.308)
if module is None:[](#l2.309)
_make_class_unpicklable(enum_class)[](#l2.310)
else:[](#l2.311)
enum_class.__module__ = module[](#l2.312)
return enum_class[](#l2.314)
- @staticmethod
- def get_mixins(bases):
"""Returns the type for creating enum members, and the first inherited[](#l2.318)
enum class.[](#l2.319)
bases: the tuple of bases that was given to __new__[](#l2.321)
"""[](#l2.323)
if not bases:[](#l2.324)
return object, Enum[](#l2.325)
# double check that we are not subclassing a class with existing[](#l2.327)
# enumeration members; while we're at it, see if any other data[](#l2.328)
# type has been mixed in so we can use the correct __new__[](#l2.329)
member_type = first_enum = None[](#l2.330)
for base in bases:[](#l2.331)
if (base is not Enum and[](#l2.332)
issubclass(base, Enum) and[](#l2.333)
base._member_names):[](#l2.334)
raise TypeError("Cannot extend enumerations")[](#l2.335)
# base is now the last base in bases[](#l2.336)
if not issubclass(base, Enum):[](#l2.337)
raise TypeError("new enumerations must be created as "[](#l2.338)
"`ClassName([mixin_type,] enum_type)`")[](#l2.339)
# get correct mix-in type (either mix-in type of Enum subclass, or[](#l2.341)
# first base if last base is Enum)[](#l2.342)
if not issubclass(bases[0], Enum):[](#l2.343)
member_type = bases[0] # first data type[](#l2.344)
first_enum = bases[-1] # enum type[](#l2.345)
else:[](#l2.346)
for base in bases[0].__mro__:[](#l2.347)
# most common: (IntEnum, int, Enum, object)[](#l2.348)
# possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>,[](#l2.349)
# <class 'int'>, <Enum 'Enum'>,[](#l2.350)
# <class 'object'>)[](#l2.351)
if issubclass(base, Enum):[](#l2.352)
if first_enum is None:[](#l2.353)
first_enum = base[](#l2.354)
else:[](#l2.355)
if member_type is None:[](#l2.356)
member_type = base[](#l2.357)
return member_type, first_enum[](#l2.359)
- @staticmethod
- def find_new(classdict, member_type, first_enum):
"""Returns the __new__ to be used for creating the enum members.[](#l2.363)
classdict: the class dictionary given to __new__[](#l2.365)
member_type: the data type whose __new__ will be used by default[](#l2.366)
first_enum: enumeration to check for an overriding __new__[](#l2.367)
"""[](#l2.369)
# now find the correct __new__, checking to see of one was defined[](#l2.370)
# by the user; also check earlier enum classes in case a __new__ was[](#l2.371)
# saved as __new_member__[](#l2.372)
__new__ = classdict.get('__new__', None)[](#l2.373)
# should __new__ be saved as __new_member__ later?[](#l2.375)
save_new = __new__ is not None[](#l2.376)
if __new__ is None:[](#l2.378)
# check all possibles for __new_member__ before falling back to[](#l2.379)
# __new__[](#l2.380)
for method in ('__new_member__', '__new__'):[](#l2.381)
for possible in (member_type, first_enum):[](#l2.382)
target = getattr(possible, method, None)[](#l2.383)
if target not in {[](#l2.384)
None,[](#l2.385)
None.__new__,[](#l2.386)
object.__new__,[](#l2.387)
Enum.__new__,[](#l2.388)
}:[](#l2.389)
__new__ = target[](#l2.390)
break[](#l2.391)
if __new__ is not None:[](#l2.392)
break[](#l2.393)
else:[](#l2.394)
__new__ = object.__new__[](#l2.395)
# if a non-object.__new__ is used then whatever value/tuple was[](#l2.397)
# assigned to the enum member name will be passed to __new__ and to the[](#l2.398)
# new enum member's __init__[](#l2.399)
if __new__ is object.__new__:[](#l2.400)
use_args = False[](#l2.401)
else:[](#l2.402)
use_args = True[](#l2.403)
return __new__, save_new, use_args[](#l2.405)
+ + +class Enum(metaclass=EnumMeta):
- """
- def new(cls, value):
# all enum instances are actually created during class construction[](#l2.415)
# without calling this method; this method is called by the metaclass'[](#l2.416)
# __call__ (i.e. Color(3) ), and by pickle[](#l2.417)
if type(value) is cls:[](#l2.418)
# For lookups like Color(Color.red)[](#l2.419)
return value[](#l2.420)
# by-value search for a matching enum member[](#l2.421)
# see if it's in the reverse mapping (for hashable values)[](#l2.422)
if value in cls._value2member_map:[](#l2.423)
return cls._value2member_map[value][](#l2.424)
# not there, now do long search -- O(n) behavior[](#l2.425)
for member in cls._member_map.values():[](#l2.426)
if member.value == value:[](#l2.427)
return member[](#l2.428)
raise ValueError("%s is not a valid %s" % (value, cls.__name__))[](#l2.429)
- def repr(self):
return "<%s.%s: %r>" % ([](#l2.432)
self.__class__.__name__, self._name, self._value)[](#l2.433)
- def eq(self, other):
if type(other) is self.__class__:[](#l2.442)
return self is other[](#l2.443)
return NotImplemented[](#l2.444)
_RouteClassAttributeToGetattr is used to provide access to thename
andvalue
properties of enum members while keeping some measure ofprotection from modification, while still allowing for an enumeration
to have members namedname
andvalue
. This works because enumerationmembers are not set directly on the enum class -- getattr is
used to look them up.
+ + +class IntEnum(int, Enum):
new file mode 100644 --- /dev/null +++ b/Lib/test/test_enum.py @@ -0,0 +1,921 @@ +import enum +import unittest +from collections import OrderedDict +from pickle import dumps, loads, PicklingError +from enum import Enum, IntEnum + +# for pickle tests +try:
- class FloatStooges(float, Enum):
LARRY = 1.39[](#l3.30)
CURLY = 2.72[](#l3.31)
MOE = 3.142596[](#l3.32)
+ +# for pickle test and subclass tests +try:
- class StrEnum(str, Enum):
'accepts only string values'[](#l3.39)
- class Name(StrEnum):
BDFL = 'Guido van Rossum'[](#l3.41)
FLUFL = 'Barry Warsaw'[](#l3.42)
+ +class TestEnum(unittest.TestCase):
- def setUp(self):
class Season(Enum):[](#l3.67)
SPRING = 1[](#l3.68)
SUMMER = 2[](#l3.69)
AUTUMN = 3[](#l3.70)
WINTER = 4[](#l3.71)
self.Season = Season[](#l3.72)
- def test_enum_in_enum_out(self):
Season = self.Season[](#l3.75)
self.assertIs(Season(Season.WINTER), Season.WINTER)[](#l3.76)
- def test_enum_value(self):
Season = self.Season[](#l3.79)
self.assertEqual(Season.SPRING.value, 1)[](#l3.80)
- def test_dir_on_class(self):
Season = self.Season[](#l3.86)
self.assertEqual([](#l3.87)
set(dir(Season)),[](#l3.88)
set(['__class__', '__doc__', '__members__',[](#l3.89)
'SPRING', 'SUMMER', 'AUTUMN', 'WINTER']),[](#l3.90)
)[](#l3.91)
- def test_dir_on_item(self):
Season = self.Season[](#l3.94)
self.assertEqual([](#l3.95)
set(dir(Season.WINTER)),[](#l3.96)
set(['__class__', '__doc__', 'name', 'value']),[](#l3.97)
)[](#l3.98)
- def test_enum(self):
Season = self.Season[](#l3.101)
lst = list(Season)[](#l3.102)
self.assertEqual(len(lst), len(Season))[](#l3.103)
self.assertEqual(len(Season), 4, Season)[](#l3.104)
self.assertEqual([](#l3.105)
[Season.SPRING, Season.SUMMER, Season.AUTUMN, Season.WINTER], lst)[](#l3.106)
for i, season in enumerate('SPRING SUMMER AUTUMN WINTER'.split(), 1):[](#l3.108)
e = Season(i)[](#l3.109)
self.assertEqual(e, getattr(Season, season))[](#l3.110)
self.assertEqual(e.value, i)[](#l3.111)
self.assertNotEqual(e, i)[](#l3.112)
self.assertEqual(e.name, season)[](#l3.113)
self.assertIn(e, Season)[](#l3.114)
self.assertIs(type(e), Season)[](#l3.115)
self.assertIsInstance(e, Season)[](#l3.116)
self.assertEqual(str(e), 'Season.' + season)[](#l3.117)
self.assertEqual([](#l3.118)
repr(e),[](#l3.119)
'<Season.{0}: {1}>'.format(season, i),[](#l3.120)
)[](#l3.121)
- def test_value_name(self):
Season = self.Season[](#l3.124)
self.assertEqual(Season.SPRING.name, 'SPRING')[](#l3.125)
self.assertEqual(Season.SPRING.value, 1)[](#l3.126)
with self.assertRaises(AttributeError):[](#l3.127)
Season.SPRING.name = 'invierno'[](#l3.128)
with self.assertRaises(AttributeError):[](#l3.129)
Season.SPRING.value = 2[](#l3.130)
- def test_invalid_names(self):
with self.assertRaises(ValueError):[](#l3.133)
class Wrong(Enum):[](#l3.134)
mro = 9[](#l3.135)
with self.assertRaises(ValueError):[](#l3.136)
class Wrong(Enum):[](#l3.137)
_create_= 11[](#l3.138)
with self.assertRaises(ValueError):[](#l3.139)
class Wrong(Enum):[](#l3.140)
_get_mixins_ = 9[](#l3.141)
with self.assertRaises(ValueError):[](#l3.142)
class Wrong(Enum):[](#l3.143)
_find_new_ = 1[](#l3.144)
with self.assertRaises(ValueError):[](#l3.145)
class Wrong(Enum):[](#l3.146)
_any_name_ = 9[](#l3.147)
- def test_contains(self):
Season = self.Season[](#l3.150)
self.assertIn(Season.AUTUMN, Season)[](#l3.151)
self.assertNotIn(3, Season)[](#l3.152)
val = Season(3)[](#l3.154)
self.assertIn(val, Season)[](#l3.155)
class OtherEnum(Enum):[](#l3.157)
one = 1; two = 2[](#l3.158)
self.assertNotIn(OtherEnum.two, Season)[](#l3.159)
- def test_comparisons(self):
Season = self.Season[](#l3.162)
with self.assertRaises(TypeError):[](#l3.163)
Season.SPRING < Season.WINTER[](#l3.164)
with self.assertRaises(TypeError):[](#l3.165)
Season.SPRING > 4[](#l3.166)
self.assertNotEqual(Season.SPRING, 1)[](#l3.168)
class Part(Enum):[](#l3.170)
SPRING = 1[](#l3.171)
CLIP = 2[](#l3.172)
BARREL = 3[](#l3.173)
self.assertNotEqual(Season.SPRING, Part.SPRING)[](#l3.175)
with self.assertRaises(TypeError):[](#l3.176)
Season.SPRING < Part.CLIP[](#l3.177)
- def test_enum_duplicates(self):
class Season(Enum):[](#l3.180)
SPRING = 1[](#l3.181)
SUMMER = 2[](#l3.182)
AUTUMN = FALL = 3[](#l3.183)
WINTER = 4[](#l3.184)
ANOTHER_SPRING = 1[](#l3.185)
lst = list(Season)[](#l3.186)
self.assertEqual([](#l3.187)
lst,[](#l3.188)
[Season.SPRING, Season.SUMMER,[](#l3.189)
Season.AUTUMN, Season.WINTER,[](#l3.190)
])[](#l3.191)
self.assertIs(Season.FALL, Season.AUTUMN)[](#l3.192)
self.assertEqual(Season.FALL.value, 3)[](#l3.193)
self.assertEqual(Season.AUTUMN.value, 3)[](#l3.194)
self.assertIs(Season(3), Season.AUTUMN)[](#l3.195)
self.assertIs(Season(1), Season.SPRING)[](#l3.196)
self.assertEqual(Season.FALL.name, 'AUTUMN')[](#l3.197)
self.assertEqual([](#l3.198)
[k for k,v in Season.__members__.items() if v.name != k],[](#l3.199)
['FALL', 'ANOTHER_SPRING'],[](#l3.200)
)[](#l3.201)
- def test_enum_with_value_name(self):
class Huh(Enum):[](#l3.204)
name = 1[](#l3.205)
value = 2[](#l3.206)
self.assertEqual([](#l3.207)
list(Huh),[](#l3.208)
[Huh.name, Huh.value],[](#l3.209)
)[](#l3.210)
self.assertIs(type(Huh.name), Huh)[](#l3.211)
self.assertEqual(Huh.name.name, 'name')[](#l3.212)
self.assertEqual(Huh.name.value, 1)[](#l3.213)
- def test_hash(self):
Season = self.Season[](#l3.215)
dates = {}[](#l3.216)
dates[Season.WINTER] = '1225'[](#l3.217)
dates[Season.SPRING] = '0315'[](#l3.218)
dates[Season.SUMMER] = '0704'[](#l3.219)
dates[Season.AUTUMN] = '1031'[](#l3.220)
self.assertEqual(dates[Season.AUTUMN], '1031')[](#l3.221)
- def test_intenum_from_scratch(self):
class phy(int, Enum):[](#l3.224)
pi = 3[](#l3.225)
tau = 2 * pi[](#l3.226)
self.assertTrue(phy.pi < phy.tau)[](#l3.227)
- def test_intenum_inherited(self):
class IntEnum(int, Enum):[](#l3.230)
pass[](#l3.231)
class phy(IntEnum):[](#l3.232)
pi = 3[](#l3.233)
tau = 2 * pi[](#l3.234)
self.assertTrue(phy.pi < phy.tau)[](#l3.235)
- def test_floatenum_from_scratch(self):
class phy(float, Enum):[](#l3.238)
pi = 3.141596[](#l3.239)
tau = 2 * pi[](#l3.240)
self.assertTrue(phy.pi < phy.tau)[](#l3.241)
- def test_floatenum_inherited(self):
class FloatEnum(float, Enum):[](#l3.244)
pass[](#l3.245)
class phy(FloatEnum):[](#l3.246)
pi = 3.141596[](#l3.247)
tau = 2 * pi[](#l3.248)
self.assertTrue(phy.pi < phy.tau)[](#l3.249)
- def test_strenum_from_scratch(self):
class phy(str, Enum):[](#l3.252)
pi = 'Pi'[](#l3.253)
tau = 'Tau'[](#l3.254)
self.assertTrue(phy.pi < phy.tau)[](#l3.255)
- def test_strenum_inherited(self):
class StrEnum(str, Enum):[](#l3.258)
pass[](#l3.259)
class phy(StrEnum):[](#l3.260)
pi = 'Pi'[](#l3.261)
tau = 'Tau'[](#l3.262)
self.assertTrue(phy.pi < phy.tau)[](#l3.263)
- def test_intenum(self):
class WeekDay(IntEnum):[](#l3.267)
SUNDAY = 1[](#l3.268)
MONDAY = 2[](#l3.269)
TUESDAY = 3[](#l3.270)
WEDNESDAY = 4[](#l3.271)
THURSDAY = 5[](#l3.272)
FRIDAY = 6[](#l3.273)
SATURDAY = 7[](#l3.274)
self.assertEqual(['a', 'b', 'c'][WeekDay.MONDAY], 'c')[](#l3.276)
self.assertEqual([i for i in range(WeekDay.TUESDAY)], [0, 1, 2])[](#l3.277)
lst = list(WeekDay)[](#l3.279)
self.assertEqual(len(lst), len(WeekDay))[](#l3.280)
self.assertEqual(len(WeekDay), 7)[](#l3.281)
target = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY'[](#l3.282)
target = target.split()[](#l3.283)
for i, weekday in enumerate(target, 1):[](#l3.284)
e = WeekDay(i)[](#l3.285)
self.assertEqual(e, i)[](#l3.286)
self.assertEqual(int(e), i)[](#l3.287)
self.assertEqual(e.name, weekday)[](#l3.288)
self.assertIn(e, WeekDay)[](#l3.289)
self.assertEqual(lst.index(e)+1, i)[](#l3.290)
self.assertTrue(0 < e < 8)[](#l3.291)
self.assertIs(type(e), WeekDay)[](#l3.292)
self.assertIsInstance(e, int)[](#l3.293)
self.assertIsInstance(e, Enum)[](#l3.294)
- def test_intenum_duplicates(self):
class WeekDay(IntEnum):[](#l3.297)
SUNDAY = 1[](#l3.298)
MONDAY = 2[](#l3.299)
TUESDAY = TEUSDAY = 3[](#l3.300)
WEDNESDAY = 4[](#l3.301)
THURSDAY = 5[](#l3.302)
FRIDAY = 6[](#l3.303)
SATURDAY = 7[](#l3.304)
self.assertIs(WeekDay.TEUSDAY, WeekDay.TUESDAY)[](#l3.305)
self.assertEqual(WeekDay(3).name, 'TUESDAY')[](#l3.306)
self.assertEqual([k for k,v in WeekDay.__members__.items()[](#l3.307)
if v.name != k], ['TEUSDAY', ])[](#l3.308)
- def test_pickle_enum(self):
if isinstance(Stooges, Exception):[](#l3.311)
raise Stooges[](#l3.312)
self.assertIs(Stooges.CURLY, loads(dumps(Stooges.CURLY)))[](#l3.313)
self.assertIs(Stooges, loads(dumps(Stooges)))[](#l3.314)
- def test_pickle_int(self):
if isinstance(IntStooges, Exception):[](#l3.317)
raise IntStooges[](#l3.318)
self.assertIs(IntStooges.CURLY, loads(dumps(IntStooges.CURLY)))[](#l3.319)
self.assertIs(IntStooges, loads(dumps(IntStooges)))[](#l3.320)
- def test_pickle_float(self):
if isinstance(FloatStooges, Exception):[](#l3.323)
raise FloatStooges[](#l3.324)
self.assertIs(FloatStooges.CURLY, loads(dumps(FloatStooges.CURLY)))[](#l3.325)
self.assertIs(FloatStooges, loads(dumps(FloatStooges)))[](#l3.326)
- def test_pickle_enum_function(self):
if isinstance(Answer, Exception):[](#l3.329)
raise Answer[](#l3.330)
self.assertIs(Answer.him, loads(dumps(Answer.him)))[](#l3.331)
self.assertIs(Answer, loads(dumps(Answer)))[](#l3.332)
- def test_pickle_enum_function_with_module(self):
if isinstance(Question, Exception):[](#l3.335)
raise Question[](#l3.336)
self.assertIs(Question.who, loads(dumps(Question.who)))[](#l3.337)
self.assertIs(Question, loads(dumps(Question)))[](#l3.338)
- def test_exploding_pickle(self):
BadPickle = Enum('BadPickle', 'dill sweet bread-n-butter')[](#l3.341)
enum._make_class_unpicklable(BadPickle)[](#l3.342)
globals()['BadPickle'] = BadPickle[](#l3.343)
with self.assertRaises(TypeError):[](#l3.344)
dumps(BadPickle.dill)[](#l3.345)
with self.assertRaises(PicklingError):[](#l3.346)
dumps(BadPickle)[](#l3.347)
- def test_string_enum(self):
class SkillLevel(str, Enum):[](#l3.350)
master = 'what is the sound of one hand clapping?'[](#l3.351)
journeyman = 'why did the chicken cross the road?'[](#l3.352)
apprentice = 'knock, knock!'[](#l3.353)
self.assertEqual(SkillLevel.apprentice, 'knock, knock!')[](#l3.354)
- def test_getattr_getitem(self):
class Period(Enum):[](#l3.357)
morning = 1[](#l3.358)
noon = 2[](#l3.359)
evening = 3[](#l3.360)
night = 4[](#l3.361)
self.assertIs(Period(2), Period.noon)[](#l3.362)
self.assertIs(getattr(Period, 'night'), Period.night)[](#l3.363)
self.assertIs(Period['morning'], Period.morning)[](#l3.364)
- def test_getattr_dunder(self):
Season = self.Season[](#l3.367)
self.assertTrue(getattr(Season, '__eq__'))[](#l3.368)
- def test_iteration_order(self):
class Season(Enum):[](#l3.371)
SUMMER = 2[](#l3.372)
WINTER = 4[](#l3.373)
AUTUMN = 3[](#l3.374)
SPRING = 1[](#l3.375)
self.assertEqual([](#l3.376)
list(Season),[](#l3.377)
[Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],[](#l3.378)
)[](#l3.379)
- def test_programatic_function_string(self):
SummerMonth = Enum('SummerMonth', 'june july august')[](#l3.382)
lst = list(SummerMonth)[](#l3.383)
self.assertEqual(len(lst), len(SummerMonth))[](#l3.384)
self.assertEqual(len(SummerMonth), 3, SummerMonth)[](#l3.385)
self.assertEqual([](#l3.386)
[SummerMonth.june, SummerMonth.july, SummerMonth.august],[](#l3.387)
lst,[](#l3.388)
)[](#l3.389)
for i, month in enumerate('june july august'.split(), 1):[](#l3.390)
e = SummerMonth(i)[](#l3.391)
self.assertEqual(int(e.value), i)[](#l3.392)
self.assertNotEqual(e, i)[](#l3.393)
self.assertEqual(e.name, month)[](#l3.394)
self.assertIn(e, SummerMonth)[](#l3.395)
self.assertIs(type(e), SummerMonth)[](#l3.396)
- def test_programatic_function_string_list(self):
SummerMonth = Enum('SummerMonth', ['june', 'july', 'august'])[](#l3.399)
lst = list(SummerMonth)[](#l3.400)
self.assertEqual(len(lst), len(SummerMonth))[](#l3.401)
self.assertEqual(len(SummerMonth), 3, SummerMonth)[](#l3.402)
self.assertEqual([](#l3.403)
[SummerMonth.june, SummerMonth.july, SummerMonth.august],[](#l3.404)
lst,[](#l3.405)
)[](#l3.406)
for i, month in enumerate('june july august'.split(), 1):[](#l3.407)
e = SummerMonth(i)[](#l3.408)
self.assertEqual(int(e.value), i)[](#l3.409)
self.assertNotEqual(e, i)[](#l3.410)
self.assertEqual(e.name, month)[](#l3.411)
self.assertIn(e, SummerMonth)[](#l3.412)
self.assertIs(type(e), SummerMonth)[](#l3.413)
- def test_programatic_function_iterable(self):
SummerMonth = Enum([](#l3.416)
'SummerMonth',[](#l3.417)
(('june', 1), ('july', 2), ('august', 3))[](#l3.418)
)[](#l3.419)
lst = list(SummerMonth)[](#l3.420)
self.assertEqual(len(lst), len(SummerMonth))[](#l3.421)
self.assertEqual(len(SummerMonth), 3, SummerMonth)[](#l3.422)
self.assertEqual([](#l3.423)
[SummerMonth.june, SummerMonth.july, SummerMonth.august],[](#l3.424)
lst,[](#l3.425)
)[](#l3.426)
for i, month in enumerate('june july august'.split(), 1):[](#l3.427)
e = SummerMonth(i)[](#l3.428)
self.assertEqual(int(e.value), i)[](#l3.429)
self.assertNotEqual(e, i)[](#l3.430)
self.assertEqual(e.name, month)[](#l3.431)
self.assertIn(e, SummerMonth)[](#l3.432)
self.assertIs(type(e), SummerMonth)[](#l3.433)
- def test_programatic_function_from_dict(self):
SummerMonth = Enum([](#l3.436)
'SummerMonth',[](#l3.437)
OrderedDict((('june', 1), ('july', 2), ('august', 3)))[](#l3.438)
)[](#l3.439)
lst = list(SummerMonth)[](#l3.440)
self.assertEqual(len(lst), len(SummerMonth))[](#l3.441)
self.assertEqual(len(SummerMonth), 3, SummerMonth)[](#l3.442)
self.assertEqual([](#l3.443)
[SummerMonth.june, SummerMonth.july, SummerMonth.august],[](#l3.444)
lst,[](#l3.445)
)[](#l3.446)
for i, month in enumerate('june july august'.split(), 1):[](#l3.447)
e = SummerMonth(i)[](#l3.448)
self.assertEqual(int(e.value), i)[](#l3.449)
self.assertNotEqual(e, i)[](#l3.450)
self.assertEqual(e.name, month)[](#l3.451)
self.assertIn(e, SummerMonth)[](#l3.452)
self.assertIs(type(e), SummerMonth)[](#l3.453)
- def test_programatic_function_type(self):
SummerMonth = Enum('SummerMonth', 'june july august', type=int)[](#l3.456)
lst = list(SummerMonth)[](#l3.457)
self.assertEqual(len(lst), len(SummerMonth))[](#l3.458)
self.assertEqual(len(SummerMonth), 3, SummerMonth)[](#l3.459)
self.assertEqual([](#l3.460)
[SummerMonth.june, SummerMonth.july, SummerMonth.august],[](#l3.461)
lst,[](#l3.462)
)[](#l3.463)
for i, month in enumerate('june july august'.split(), 1):[](#l3.464)
e = SummerMonth(i)[](#l3.465)
self.assertEqual(e, i)[](#l3.466)
self.assertEqual(e.name, month)[](#l3.467)
self.assertIn(e, SummerMonth)[](#l3.468)
self.assertIs(type(e), SummerMonth)[](#l3.469)
- def test_programatic_function_type_from_subclass(self):
SummerMonth = IntEnum('SummerMonth', 'june july august')[](#l3.472)
lst = list(SummerMonth)[](#l3.473)
self.assertEqual(len(lst), len(SummerMonth))[](#l3.474)
self.assertEqual(len(SummerMonth), 3, SummerMonth)[](#l3.475)
self.assertEqual([](#l3.476)
[SummerMonth.june, SummerMonth.july, SummerMonth.august],[](#l3.477)
lst,[](#l3.478)
)[](#l3.479)
for i, month in enumerate('june july august'.split(), 1):[](#l3.480)
e = SummerMonth(i)[](#l3.481)
self.assertEqual(e, i)[](#l3.482)
self.assertEqual(e.name, month)[](#l3.483)
self.assertIn(e, SummerMonth)[](#l3.484)
self.assertIs(type(e), SummerMonth)[](#l3.485)
- def test_subclassing(self):
if isinstance(Name, Exception):[](#l3.488)
raise Name[](#l3.489)
self.assertEqual(Name.BDFL, 'Guido van Rossum')[](#l3.490)
self.assertTrue(Name.BDFL, Name('Guido van Rossum'))[](#l3.491)
self.assertIs(Name.BDFL, getattr(Name, 'BDFL'))[](#l3.492)
self.assertIs(Name.BDFL, loads(dumps(Name.BDFL)))[](#l3.493)
- def test_extending(self):
class Color(Enum):[](#l3.496)
red = 1[](#l3.497)
green = 2[](#l3.498)
blue = 3[](#l3.499)
with self.assertRaises(TypeError):[](#l3.500)
class MoreColor(Color):[](#l3.501)
cyan = 4[](#l3.502)
magenta = 5[](#l3.503)
yellow = 6[](#l3.504)
- def test_exclude_methods(self):
class whatever(Enum):[](#l3.507)
this = 'that'[](#l3.508)
these = 'those'[](#l3.509)
def really(self):[](#l3.510)
return 'no, not %s' % self.value[](#l3.511)
self.assertIsNot(type(whatever.really), whatever)[](#l3.512)
self.assertEqual(whatever.this.really(), 'no, not that')[](#l3.513)
- def test_overwrite_enums(self):
class Why(Enum):[](#l3.516)
question = 1[](#l3.517)
answer = 2[](#l3.518)
propisition = 3[](#l3.519)
def question(self):[](#l3.520)
print(42)[](#l3.521)
self.assertIsNot(type(Why.question), Why)[](#l3.522)
self.assertNotIn(Why.question, Why._member_names)[](#l3.523)
self.assertNotIn(Why.question, Why)[](#l3.524)
- def test_wrong_inheritance_order(self):
with self.assertRaises(TypeError):[](#l3.527)
class Wrong(Enum, str):[](#l3.528)
NotHere = 'error before this point'[](#l3.529)
- def test_intenum_transitivity(self):
class number(IntEnum):[](#l3.532)
one = 1[](#l3.533)
two = 2[](#l3.534)
three = 3[](#l3.535)
class numero(IntEnum):[](#l3.536)
uno = 1[](#l3.537)
dos = 2[](#l3.538)
tres = 3[](#l3.539)
self.assertEqual(number.one, numero.uno)[](#l3.540)
self.assertEqual(number.two, numero.dos)[](#l3.541)
self.assertEqual(number.three, numero.tres)[](#l3.542)
- def test_wrong_enum_in_call(self):
class Monochrome(Enum):[](#l3.545)
black = 0[](#l3.546)
white = 1[](#l3.547)
class Gender(Enum):[](#l3.548)
male = 0[](#l3.549)
female = 1[](#l3.550)
self.assertRaises(ValueError, Monochrome, Gender.male)[](#l3.551)
- def test_wrong_enum_in_mixed_call(self):
class Monochrome(IntEnum):[](#l3.554)
black = 0[](#l3.555)
white = 1[](#l3.556)
class Gender(Enum):[](#l3.557)
male = 0[](#l3.558)
female = 1[](#l3.559)
self.assertRaises(ValueError, Monochrome, Gender.male)[](#l3.560)
- def test_mixed_enum_in_call_1(self):
class Monochrome(IntEnum):[](#l3.563)
black = 0[](#l3.564)
white = 1[](#l3.565)
class Gender(IntEnum):[](#l3.566)
male = 0[](#l3.567)
female = 1[](#l3.568)
self.assertIs(Monochrome(Gender.female), Monochrome.white)[](#l3.569)
- def test_mixed_enum_in_call_2(self):
class Monochrome(Enum):[](#l3.572)
black = 0[](#l3.573)
white = 1[](#l3.574)
class Gender(IntEnum):[](#l3.575)
male = 0[](#l3.576)
female = 1[](#l3.577)
self.assertIs(Monochrome(Gender.male), Monochrome.black)[](#l3.578)
- def test_flufl_enum(self):
class Fluflnum(Enum):[](#l3.581)
def __int__(self):[](#l3.582)
return int(self.value)[](#l3.583)
class MailManOptions(Fluflnum):[](#l3.584)
option1 = 1[](#l3.585)
option2 = 2[](#l3.586)
option3 = 3[](#l3.587)
self.assertEqual(int(MailManOptions.option1), 1)[](#l3.588)
- def test_no_such_enum_member(self):
class Color(Enum):[](#l3.591)
red = 1[](#l3.592)
green = 2[](#l3.593)
blue = 3[](#l3.594)
with self.assertRaises(ValueError):[](#l3.595)
Color(4)[](#l3.596)
with self.assertRaises(KeyError):[](#l3.597)
Color['chartreuse'][](#l3.598)
- def test_new_repr(self):
class Color(Enum):[](#l3.601)
red = 1[](#l3.602)
green = 2[](#l3.603)
blue = 3[](#l3.604)
def __repr__(self):[](#l3.605)
return "don't you just love shades of %s?" % self.name[](#l3.606)
self.assertEqual([](#l3.607)
repr(Color.blue),[](#l3.608)
"don't you just love shades of blue?",[](#l3.609)
)[](#l3.610)
- def test_inherited_repr(self):
class MyEnum(Enum):[](#l3.613)
def __repr__(self):[](#l3.614)
return "My name is %s." % self.name[](#l3.615)
class MyIntEnum(int, MyEnum):[](#l3.616)
this = 1[](#l3.617)
that = 2[](#l3.618)
theother = 3[](#l3.619)
self.assertEqual(repr(MyIntEnum.that), "My name is that.")[](#l3.620)
- def test_multiple_mixin_mro(self):
class auto_enum(type(Enum)):[](#l3.623)
def __new__(metacls, cls, bases, classdict):[](#l3.624)
temp = type(classdict)()[](#l3.625)
names = set(classdict._member_names)[](#l3.626)
i = 0[](#l3.627)
for k in classdict._member_names:[](#l3.628)
v = classdict[k][](#l3.629)
if v is Ellipsis:[](#l3.630)
v = i[](#l3.631)
else:[](#l3.632)
i = v[](#l3.633)
i += 1[](#l3.634)
temp[k] = v[](#l3.635)
for k, v in classdict.items():[](#l3.636)
if k not in names:[](#l3.637)
temp[k] = v[](#l3.638)
return super(auto_enum, metacls).__new__([](#l3.639)
metacls, cls, bases, temp)[](#l3.640)
class AutoNumberedEnum(Enum, metaclass=auto_enum):[](#l3.642)
pass[](#l3.643)
class AutoIntEnum(IntEnum, metaclass=auto_enum):[](#l3.645)
pass[](#l3.646)
class TestAutoNumber(AutoNumberedEnum):[](#l3.648)
a = ...[](#l3.649)
b = 3[](#l3.650)
c = ...[](#l3.651)
class TestAutoInt(AutoIntEnum):[](#l3.653)
a = ...[](#l3.654)
b = 3[](#l3.655)
c = ...[](#l3.656)
- def test_subclasses_with_getnewargs(self):
class NamedInt(int):[](#l3.659)
def __new__(cls, *args):[](#l3.660)
_args = args[](#l3.661)
name, *args = args[](#l3.662)
if len(args) == 0:[](#l3.663)
raise TypeError("name and value must be specified")[](#l3.664)
self = int.__new__(cls, *args)[](#l3.665)
self._intname = name[](#l3.666)
self._args = _args[](#l3.667)
return self[](#l3.668)
def __getnewargs__(self):[](#l3.669)
return self._args[](#l3.670)
@property[](#l3.671)
def __name__(self):[](#l3.672)
return self._intname[](#l3.673)
def __repr__(self):[](#l3.674)
# repr() is updated to include the name and type info[](#l3.675)
return "{}({!r}, {})".format(type(self).__name__,[](#l3.676)
self.__name__,[](#l3.677)
int.__repr__(self))[](#l3.678)
def __str__(self):[](#l3.679)
# str() is unchanged, even if it relies on the repr() fallback[](#l3.680)
base = int[](#l3.681)
base_str = base.__str__[](#l3.682)
if base_str.__objclass__ is object:[](#l3.683)
return base.__repr__(self)[](#l3.684)
return base_str(self)[](#l3.685)
# for simplicity, we only define one operator that[](#l3.686)
# propagates expressions[](#l3.687)
def __add__(self, other):[](#l3.688)
temp = int(self) + int( other)[](#l3.689)
if isinstance(self, NamedInt) and isinstance(other, NamedInt):[](#l3.690)
return NamedInt([](#l3.691)
'({0} + {1})'.format(self.__name__, other.__name__),[](#l3.692)
temp )[](#l3.693)
else:[](#l3.694)
return temp[](#l3.695)
class NEI(NamedInt, Enum):[](#l3.697)
x = ('the-x', 1)[](#l3.698)
y = ('the-y', 2)[](#l3.699)
self.assertIs(NEI.__new__, Enum.__new__)[](#l3.701)
self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")[](#l3.702)
globals()['NamedInt'] = NamedInt[](#l3.703)
globals()['NEI'] = NEI[](#l3.704)
NI5 = NamedInt('test', 5)[](#l3.705)
self.assertEqual(NI5, 5)[](#l3.706)
self.assertEqual(loads(dumps(NI5)), 5)[](#l3.707)
self.assertEqual(NEI.y.value, 2)[](#l3.708)
self.assertIs(loads(dumps(NEI.y)), NEI.y)[](#l3.709)
- def test_subclasses_without_getnewargs(self):
class NamedInt(int):[](#l3.712)
def __new__(cls, *args):[](#l3.713)
_args = args[](#l3.714)
name, *args = args[](#l3.715)
if len(args) == 0:[](#l3.716)
raise TypeError("name and value must be specified")[](#l3.717)
self = int.__new__(cls, *args)[](#l3.718)
self._intname = name[](#l3.719)
self._args = _args[](#l3.720)
return self[](#l3.721)
@property[](#l3.722)
def __name__(self):[](#l3.723)
return self._intname[](#l3.724)
def __repr__(self):[](#l3.725)
# repr() is updated to include the name and type info[](#l3.726)
return "{}({!r}, {})".format(type(self).__name__,[](#l3.727)
self.__name__,[](#l3.728)
int.__repr__(self))[](#l3.729)
def __str__(self):[](#l3.730)
# str() is unchanged, even if it relies on the repr() fallback[](#l3.731)
base = int[](#l3.732)
base_str = base.__str__[](#l3.733)
if base_str.__objclass__ is object:[](#l3.734)
return base.__repr__(self)[](#l3.735)
return base_str(self)[](#l3.736)
# for simplicity, we only define one operator that[](#l3.737)
# propagates expressions[](#l3.738)
def __add__(self, other):[](#l3.739)
temp = int(self) + int( other)[](#l3.740)
if isinstance(self, NamedInt) and isinstance(other, NamedInt):[](#l3.741)
return NamedInt([](#l3.742)
'({0} + {1})'.format(self.__name__, other.__name__),[](#l3.743)
temp )[](#l3.744)
else:[](#l3.745)
return temp[](#l3.746)
class NEI(NamedInt, Enum):[](#l3.748)
x = ('the-x', 1)[](#l3.749)
y = ('the-y', 2)[](#l3.750)
self.assertIs(NEI.__new__, Enum.__new__)[](#l3.752)
self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")[](#l3.753)
globals()['NamedInt'] = NamedInt[](#l3.754)
globals()['NEI'] = NEI[](#l3.755)
NI5 = NamedInt('test', 5)[](#l3.756)
self.assertEqual(NI5, 5)[](#l3.757)
self.assertEqual(NEI.y.value, 2)[](#l3.758)
with self.assertRaises(TypeError):[](#l3.759)
dumps(NEI.x)[](#l3.760)
with self.assertRaises(PicklingError):[](#l3.761)
dumps(NEI)[](#l3.762)
- def test_tuple_subclass(self):
class SomeTuple(tuple, Enum):[](#l3.765)
first = (1, 'for the money')[](#l3.766)
second = (2, 'for the show')[](#l3.767)
third = (3, 'for the music')[](#l3.768)
self.assertIs(type(SomeTuple.first), SomeTuple)[](#l3.769)
self.assertIsInstance(SomeTuple.second, tuple)[](#l3.770)
self.assertEqual(SomeTuple.third, (3, 'for the music'))[](#l3.771)
globals()['SomeTuple'] = SomeTuple[](#l3.772)
self.assertIs(loads(dumps(SomeTuple.first)), SomeTuple.first)[](#l3.773)
- def test_duplicate_values_give_unique_enum_items(self):
class AutoNumber(Enum):[](#l3.776)
first = ()[](#l3.777)
second = ()[](#l3.778)
third = ()[](#l3.779)
def __new__(cls):[](#l3.780)
value = len(cls.__members__) + 1[](#l3.781)
obj = object.__new__(cls)[](#l3.782)
obj._value = value[](#l3.783)
return obj[](#l3.784)
def __int__(self):[](#l3.785)
return int(self._value)[](#l3.786)
self.assertEqual([](#l3.787)
list(AutoNumber),[](#l3.788)
[AutoNumber.first, AutoNumber.second, AutoNumber.third],[](#l3.789)
)[](#l3.790)
self.assertEqual(int(AutoNumber.second), 2)[](#l3.791)
self.assertIs(AutoNumber(1), AutoNumber.first)[](#l3.792)
- def test_inherited_new_from_enhanced_enum(self):
class AutoNumber(Enum):[](#l3.795)
def __new__(cls):[](#l3.796)
value = len(cls.__members__) + 1[](#l3.797)
obj = object.__new__(cls)[](#l3.798)
obj._value = value[](#l3.799)
return obj[](#l3.800)
def __int__(self):[](#l3.801)
return int(self._value)[](#l3.802)
class Color(AutoNumber):[](#l3.803)
red = ()[](#l3.804)
green = ()[](#l3.805)
blue = ()[](#l3.806)
self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])[](#l3.807)
self.assertEqual(list(map(int, Color)), [1, 2, 3])[](#l3.808)
- def test_inherited_new_from_mixed_enum(self):
class AutoNumber(IntEnum):[](#l3.811)
def __new__(cls):[](#l3.812)
value = len(cls.__members__) + 1[](#l3.813)
obj = int.__new__(cls, value)[](#l3.814)
obj._value = value[](#l3.815)
return obj[](#l3.816)
class Color(AutoNumber):[](#l3.817)
red = ()[](#l3.818)
green = ()[](#l3.819)
blue = ()[](#l3.820)
self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])[](#l3.821)
self.assertEqual(list(map(int, Color)), [1, 2, 3])[](#l3.822)
- def test_ordered_mixin(self):
class OrderedEnum(Enum):[](#l3.825)
def __ge__(self, other):[](#l3.826)
if self.__class__ is other.__class__:[](#l3.827)
return self._value >= other._value[](#l3.828)
return NotImplemented[](#l3.829)
def __gt__(self, other):[](#l3.830)
if self.__class__ is other.__class__:[](#l3.831)
return self._value > other._value[](#l3.832)
return NotImplemented[](#l3.833)
def __le__(self, other):[](#l3.834)
if self.__class__ is other.__class__:[](#l3.835)
return self._value <= other._value[](#l3.836)
return NotImplemented[](#l3.837)
def __lt__(self, other):[](#l3.838)
if self.__class__ is other.__class__:[](#l3.839)
return self._value < other._value[](#l3.840)
return NotImplemented[](#l3.841)
class Grade(OrderedEnum):[](#l3.842)
A = 5[](#l3.843)
B = 4[](#l3.844)
C = 3[](#l3.845)
D = 2[](#l3.846)
F = 1[](#l3.847)
self.assertGreater(Grade.A, Grade.B)[](#l3.848)
self.assertLessEqual(Grade.F, Grade.C)[](#l3.849)
self.assertLess(Grade.D, Grade.A)[](#l3.850)
self.assertGreaterEqual(Grade.B, Grade.B)[](#l3.851)
- def test_extending2(self):
class Shade(Enum):[](#l3.853)
def shade(self):[](#l3.854)
print(self.name)[](#l3.855)
class Color(Shade):[](#l3.856)
red = 1[](#l3.857)
green = 2[](#l3.858)
blue = 3[](#l3.859)
with self.assertRaises(TypeError):[](#l3.860)
class MoreColor(Color):[](#l3.861)
cyan = 4[](#l3.862)
magenta = 5[](#l3.863)
yellow = 6[](#l3.864)
- def test_extending3(self):
class Shade(Enum):[](#l3.867)
def shade(self):[](#l3.868)
return self.name[](#l3.869)
class Color(Shade):[](#l3.870)
def hex(self):[](#l3.871)
return '%s hexlified!' % self.value[](#l3.872)
class MoreColor(Color):[](#l3.873)
cyan = 4[](#l3.874)
magenta = 5[](#l3.875)
yellow = 6[](#l3.876)
self.assertEqual(MoreColor.magenta.hex(), '5 hexlified!')[](#l3.877)
- def test_no_duplicates(self):
class UniqueEnum(Enum):[](#l3.881)
def __init__(self, *args):[](#l3.882)
cls = self.__class__[](#l3.883)
if any(self.value == e.value for e in cls):[](#l3.884)
a = self.name[](#l3.885)
e = cls(self.value).name[](#l3.886)
raise ValueError([](#l3.887)
"aliases not allowed in UniqueEnum: %r --> %r"[](#l3.888)
% (a, e)[](#l3.889)
)[](#l3.890)
class Color(UniqueEnum):[](#l3.891)
red = 1[](#l3.892)
green = 2[](#l3.893)
blue = 3[](#l3.894)
with self.assertRaises(ValueError):[](#l3.895)
class Color(UniqueEnum):[](#l3.896)
red = 1[](#l3.897)
green = 2[](#l3.898)
blue = 3[](#l3.899)
grene = 2[](#l3.900)
- def test_init(self):
class Planet(Enum):[](#l3.903)
MERCURY = (3.303e+23, 2.4397e6)[](#l3.904)
VENUS = (4.869e+24, 6.0518e6)[](#l3.905)
EARTH = (5.976e+24, 6.37814e6)[](#l3.906)
MARS = (6.421e+23, 3.3972e6)[](#l3.907)
JUPITER = (1.9e+27, 7.1492e7)[](#l3.908)
SATURN = (5.688e+26, 6.0268e7)[](#l3.909)
URANUS = (8.686e+25, 2.5559e7)[](#l3.910)
NEPTUNE = (1.024e+26, 2.4746e7)[](#l3.911)
def __init__(self, mass, radius):[](#l3.912)
self.mass = mass # in kilograms[](#l3.913)
self.radius = radius # in meters[](#l3.914)
@property[](#l3.915)
def surface_gravity(self):[](#l3.916)
# universal gravitational constant (m3 kg-1 s-2)[](#l3.917)
G = 6.67300E-11[](#l3.918)
return G * self.mass / (self.radius * self.radius)[](#l3.919)
self.assertEqual(round(Planet.EARTH.surface_gravity, 2), 9.80)[](#l3.920)
self.assertEqual(Planet.EARTH.value, (5.976e+24, 6.37814e6))[](#l3.921)