Mercurial > cpython-lru_cache-weakref
annotate Lib/enum.py @ 86127:a3d09c19bb93
merge with 3.3
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sun, 06 Oct 2013 19:24:00 +0200 |
| parents | 96081e7526f0 |
| children | 516576f5f9dc |
| rev | line source |
|---|---|
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1 import sys |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
2 from collections import OrderedDict |
|
85796
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
3 from types import MappingProxyType, DynamicClassAttribute |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
4 |
|
84713
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
5 __all__ = ['Enum', 'IntEnum', 'unique'] |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
6 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
7 |
|
85717
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
8 def _is_descriptor(obj): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
9 """Returns True if obj is a descriptor, False otherwise.""" |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
10 return ( |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
11 hasattr(obj, '__get__') or |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
12 hasattr(obj, '__set__') or |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
13 hasattr(obj, '__delete__')) |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
14 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
15 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
16 def _is_dunder(name): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
17 """Returns True if a __dunder__ name, False otherwise.""" |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
18 return (name[:2] == name[-2:] == '__' and |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
19 name[2:3] != '_' and |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
20 name[-3:-2] != '_') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
21 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
22 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
23 def _is_sunder(name): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
24 """Returns True if a _sunder_ name, False otherwise.""" |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
25 return (name[0] == name[-1] == '_' and |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
26 name[1:2] != '_' and |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
27 name[-2:-1] != '_') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
28 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
29 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
30 def _make_class_unpicklable(cls): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
31 """Make the given class un-picklable.""" |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
32 def _break_on_call_reduce(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
33 raise TypeError('%r cannot be pickled' % self) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
34 cls.__reduce__ = _break_on_call_reduce |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
35 cls.__module__ = '<unknown>' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
36 |
|
85717
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
37 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
38 class _EnumDict(dict): |
|
85717
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
39 """Track enum member order and ensure member names are not reused. |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
40 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
41 EnumMeta will use the names found in self._member_names as the |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
42 enumeration member names. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
43 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
44 """ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
45 def __init__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
46 super().__init__() |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
47 self._member_names = [] |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
48 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
49 def __setitem__(self, key, value): |
|
85717
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
50 """Changes anything not dundered or not a descriptor. |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
51 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
52 If an enum member name is used twice, an error is raised; duplicate |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
53 values are not checked for. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
54 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
55 Single underscore (sunder) names are reserved. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
56 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
57 """ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
58 if _is_sunder(key): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
59 raise ValueError('_names_ are reserved for future Enum use') |
|
85717
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
60 elif _is_dunder(key): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
61 pass |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
62 elif key in self._member_names: |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
63 # descriptor overwriting an enum? |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
64 raise TypeError('Attempted to reuse key: %r' % key) |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
65 elif not _is_descriptor(value): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
66 if key in self: |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
67 # enum overwriting a descriptor? |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
68 raise TypeError('Key already defined as: %r' % self[key]) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
69 self._member_names.append(key) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
70 super().__setitem__(key, value) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
71 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
72 |
|
85717
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
73 |
|
85222
a9ee869cae40
#18705: merge with 3.3.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
85127
diff
changeset
|
74 # Dummy value for Enum as EnumMeta explicitly checks for it, but of course |
|
a9ee869cae40
#18705: merge with 3.3.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
85127
diff
changeset
|
75 # until EnumMeta finishes running the first time the Enum class doesn't exist. |
|
a9ee869cae40
#18705: merge with 3.3.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
85127
diff
changeset
|
76 # This is also why there are checks in EnumMeta like `if Enum is not None` |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
77 Enum = None |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
78 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
79 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
80 class EnumMeta(type): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
81 """Metaclass for Enum""" |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
82 @classmethod |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
83 def __prepare__(metacls, cls, bases): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
84 return _EnumDict() |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
85 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
86 def __new__(metacls, cls, bases, classdict): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
87 # an Enum class is final once enumeration items have been defined; it |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
88 # cannot be mixed with other types (int, float, etc.) if it has an |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
89 # inherited __new__ unless a new __new__ is defined (or the resulting |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
90 # class will fail). |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
91 member_type, first_enum = metacls._get_mixins_(bases) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
92 __new__, save_new, use_args = metacls._find_new_(classdict, member_type, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
93 first_enum) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
94 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
95 # save enum items into separate mapping so they don't get baked into |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
96 # the new class |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
97 members = {k: classdict[k] for k in classdict._member_names} |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
98 for name in classdict._member_names: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
99 del classdict[name] |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
100 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
101 # check for illegal enum names (any others?) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
102 invalid_names = set(members) & {'mro', } |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
103 if invalid_names: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
104 raise ValueError('Invalid enum member name: {0}'.format( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
105 ','.join(invalid_names))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
106 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
107 # create our new Enum type |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
108 enum_class = super().__new__(metacls, cls, bases, classdict) |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
109 enum_class._member_names_ = [] # names in definition order |
|
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
110 enum_class._member_map_ = OrderedDict() # name->value map |
|
85021
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
111 enum_class._member_type_ = member_type |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
112 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
113 # Reverse value->name map for hashable values. |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
114 enum_class._value2member_map_ = {} |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
115 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
116 # check for a __getnewargs__, and if not present sabotage |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
117 # pickling, since it won't work anyway |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
118 if (member_type is not object and |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
119 member_type.__dict__.get('__getnewargs__') is None |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
120 ): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
121 _make_class_unpicklable(enum_class) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
122 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
123 # instantiate them, checking for duplicates as we go |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
124 # we instantiate first instead of checking for duplicates first in case |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
125 # a custom __new__ is doing something funky with the values -- such as |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
126 # auto-numbering ;) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
127 for member_name in classdict._member_names: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
128 value = members[member_name] |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
129 if not isinstance(value, tuple): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
130 args = (value, ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
131 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
132 args = value |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
133 if member_type is tuple: # special case for tuple enums |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
134 args = (args, ) # wrap it one more time |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
135 if not use_args: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
136 enum_member = __new__(enum_class) |
|
84826
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
137 if not hasattr(enum_member, '_value_'): |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
138 enum_member._value_ = value |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
139 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
140 enum_member = __new__(enum_class, *args) |
|
84826
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
141 if not hasattr(enum_member, '_value_'): |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
142 enum_member._value_ = member_type(*args) |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
143 value = enum_member._value_ |
|
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
144 enum_member._name_ = member_name |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
145 enum_member.__objclass__ = enum_class |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
146 enum_member.__init__(*args) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
147 # If another member with the same value was already defined, the |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
148 # new member becomes an alias to the existing one. |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
149 for name, canonical_member in enum_class._member_map_.items(): |
|
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
150 if canonical_member.value == enum_member._value_: |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
151 enum_member = canonical_member |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
152 break |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
153 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
154 # Aliases don't appear in member names (only in __members__). |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
155 enum_class._member_names_.append(member_name) |
|
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
156 enum_class._member_map_[member_name] = enum_member |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
157 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
158 # This may fail if value is not hashable. We can't add the value |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
159 # to the map, and by-value lookups for this value will be |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
160 # linear. |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
161 enum_class._value2member_map_[value] = enum_member |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
162 except TypeError: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
163 pass |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
164 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
165 # double check that repr and friends are not the mixin's or various |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
166 # things break (such as pickle) |
|
85485
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
167 for name in ('__repr__', '__str__', '__format__', '__getnewargs__'): |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
168 class_method = getattr(enum_class, name) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
169 obj_method = getattr(member_type, name, None) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
170 enum_method = getattr(first_enum, name, None) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
171 if obj_method is not None and obj_method is class_method: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
172 setattr(enum_class, name, enum_method) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
173 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
174 # replace any other __new__ with our own (as long as Enum is not None, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
175 # anyway) -- again, this is to support pickle |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
176 if Enum is not None: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
177 # if the user defined their own __new__, save it before it gets |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
178 # clobbered in case they subclass later |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
179 if save_new: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
180 enum_class.__new_member__ = __new__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
181 enum_class.__new__ = Enum.__new__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
182 return enum_class |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
183 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
184 def __call__(cls, value, names=None, *, module=None, type=None): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
185 """Either returns an existing member, or creates a new enum class. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
186 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
187 This method is used both when an enum class is given a value to match |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
188 to an enumeration member (i.e. Color(3)) and for the functional API |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
189 (i.e. Color = Enum('Color', names='red green blue')). |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
190 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
191 When used for the functional API: `module`, if set, will be stored in |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
192 the new class' __module__ attribute; `type`, if set, will be mixed in |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
193 as the first base class. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
194 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
195 Note: if `module` is not set this routine will attempt to discover the |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
196 calling module by walking the frame stack; if this is unsuccessful |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
197 the resulting class will not be pickleable. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
198 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
199 """ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
200 if names is None: # simple value lookup |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
201 return cls.__new__(cls, value) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
202 # otherwise, functional API: we're creating a new Enum type |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
203 return cls._create_(value, names, module=module, type=type) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
204 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
205 def __contains__(cls, member): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
206 return isinstance(member, cls) and member.name in cls._member_map_ |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
207 |
|
85790
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
208 def __delattr__(cls, attr): |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
209 # nicer error message when someone tries to delete an attribute |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
210 # (see issue19025). |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
211 if attr in cls._member_map_: |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
212 raise AttributeError( |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
213 "%s: cannot delete Enum member." % cls.__name__) |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
214 super().__delattr__(attr) |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
215 |
|
85127
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
216 def __dir__(self): |
|
85790
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
217 return (['__class__', '__doc__', '__members__', '__module__'] + |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
218 self._member_names_) |
|
85127
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
219 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
220 def __getattr__(cls, name): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
221 """Return the enum member matching `name` |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
222 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
223 We use __getattr__ instead of descriptors or inserting into the enum |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
224 class' __dict__ in order to support `name` and `value` being both |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
225 properties for enum members (which live in the class' __dict__) and |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
226 enum members themselves. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
227 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
228 """ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
229 if _is_dunder(name): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
230 raise AttributeError(name) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
231 try: |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
232 return cls._member_map_[name] |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
233 except KeyError: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
234 raise AttributeError(name) from None |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
235 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
236 def __getitem__(cls, name): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
237 return cls._member_map_[name] |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
238 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
239 def __iter__(cls): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
240 return (cls._member_map_[name] for name in cls._member_names_) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
241 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
242 def __len__(cls): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
243 return len(cls._member_names_) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
244 |
|
85707
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
245 @property |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
246 def __members__(cls): |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
247 """Returns a mapping of member name->value. |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
248 |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
249 This mapping lists all enum members, including aliases. Note that this |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
250 is a read-only view of the internal mapping. |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
251 |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
252 """ |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
253 return MappingProxyType(cls._member_map_) |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
254 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
255 def __repr__(cls): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
256 return "<enum %r>" % cls.__name__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
257 |
|
85707
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
258 def __reversed__(cls): |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
259 return (cls._member_map_[name] for name in reversed(cls._member_names_)) |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
260 |
|
85574
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
261 def __setattr__(cls, name, value): |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
262 """Block attempts to reassign Enum members. |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
263 |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
264 A simple assignment to the class namespace only changes one of the |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
265 several possible ways to get an Enum member from the Enum class, |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
266 resulting in an inconsistent Enumeration. |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
267 |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
268 """ |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
269 member_map = cls.__dict__.get('_member_map_', {}) |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
270 if name in member_map: |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
271 raise AttributeError('Cannot reassign members.') |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
272 super().__setattr__(name, value) |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
273 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
274 def _create_(cls, class_name, names=None, *, module=None, type=None): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
275 """Convenience method to create a new Enum class. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
276 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
277 `names` can be: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
278 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
279 * A string containing member names, separated either with spaces or |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
280 commas. Values are auto-numbered from 1. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
281 * An iterable of member names. Values are auto-numbered from 1. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
282 * An iterable of (member name, value) pairs. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
283 * A mapping of member name -> value. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
284 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
285 """ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
286 metacls = cls.__class__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
287 bases = (cls, ) if type is None else (type, cls) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
288 classdict = metacls.__prepare__(class_name, bases) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
289 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
290 # special processing needed for names? |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
291 if isinstance(names, str): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
292 names = names.replace(',', ' ').split() |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
293 if isinstance(names, (tuple, list)) and isinstance(names[0], str): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
294 names = [(e, i) for (i, e) in enumerate(names, 1)] |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
295 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
296 # Here, names is either an iterable of (name, value) or a mapping. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
297 for item in names: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
298 if isinstance(item, str): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
299 member_name, member_value = item, names[item] |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
300 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
301 member_name, member_value = item |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
302 classdict[member_name] = member_value |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
303 enum_class = metacls.__new__(metacls, class_name, bases, classdict) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
304 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
305 # TODO: replace the frame hack if a blessed way to know the calling |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
306 # module is ever developed |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
307 if module is None: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
308 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
309 module = sys._getframe(2).f_globals['__name__'] |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
310 except (AttributeError, ValueError) as exc: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
311 pass |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
312 if module is None: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
313 _make_class_unpicklable(enum_class) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
314 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
315 enum_class.__module__ = module |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
316 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
317 return enum_class |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
318 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
319 @staticmethod |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
320 def _get_mixins_(bases): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
321 """Returns the type for creating enum members, and the first inherited |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
322 enum class. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
323 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
324 bases: the tuple of bases that was given to __new__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
325 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
326 """ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
327 if not bases: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
328 return object, Enum |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
329 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
330 # double check that we are not subclassing a class with existing |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
331 # enumeration members; while we're at it, see if any other data |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
332 # type has been mixed in so we can use the correct __new__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
333 member_type = first_enum = None |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
334 for base in bases: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
335 if (base is not Enum and |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
336 issubclass(base, Enum) and |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
337 base._member_names_): |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
338 raise TypeError("Cannot extend enumerations") |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
339 # base is now the last base in bases |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
340 if not issubclass(base, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
341 raise TypeError("new enumerations must be created as " |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
342 "`ClassName([mixin_type,] enum_type)`") |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
343 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
344 # get correct mix-in type (either mix-in type of Enum subclass, or |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
345 # first base if last base is Enum) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
346 if not issubclass(bases[0], Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
347 member_type = bases[0] # first data type |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
348 first_enum = bases[-1] # enum type |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
349 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
350 for base in bases[0].__mro__: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
351 # most common: (IntEnum, int, Enum, object) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
352 # possible: (<Enum 'AutoIntEnum'>, <Enum 'IntEnum'>, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
353 # <class 'int'>, <Enum 'Enum'>, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
354 # <class 'object'>) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
355 if issubclass(base, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
356 if first_enum is None: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
357 first_enum = base |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
358 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
359 if member_type is None: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
360 member_type = base |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
361 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
362 return member_type, first_enum |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
363 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
364 @staticmethod |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
365 def _find_new_(classdict, member_type, first_enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
366 """Returns the __new__ to be used for creating the enum members. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
367 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
368 classdict: the class dictionary given to __new__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
369 member_type: the data type whose __new__ will be used by default |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
370 first_enum: enumeration to check for an overriding __new__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
371 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
372 """ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
373 # now find the correct __new__, checking to see of one was defined |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
374 # by the user; also check earlier enum classes in case a __new__ was |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
375 # saved as __new_member__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
376 __new__ = classdict.get('__new__', None) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
377 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
378 # should __new__ be saved as __new_member__ later? |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
379 save_new = __new__ is not None |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
380 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
381 if __new__ is None: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
382 # check all possibles for __new_member__ before falling back to |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
383 # __new__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
384 for method in ('__new_member__', '__new__'): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
385 for possible in (member_type, first_enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
386 target = getattr(possible, method, None) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
387 if target not in { |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
388 None, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
389 None.__new__, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
390 object.__new__, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
391 Enum.__new__, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
392 }: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
393 __new__ = target |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
394 break |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
395 if __new__ is not None: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
396 break |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
397 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
398 __new__ = object.__new__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
399 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
400 # if a non-object.__new__ is used then whatever value/tuple was |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
401 # assigned to the enum member name will be passed to __new__ and to the |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
402 # new enum member's __init__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
403 if __new__ is object.__new__: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
404 use_args = False |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
405 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
406 use_args = True |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
407 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
408 return __new__, save_new, use_args |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
409 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
410 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
411 class Enum(metaclass=EnumMeta): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
412 """Generic enumeration. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
413 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
414 Derive from this class to define new enumerations. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
415 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
416 """ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
417 def __new__(cls, value): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
418 # all enum instances are actually created during class construction |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
419 # without calling this method; this method is called by the metaclass' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
420 # __call__ (i.e. Color(3) ), and by pickle |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
421 if type(value) is cls: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
422 # For lookups like Color(Color.red) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
423 return value |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
424 # by-value search for a matching enum member |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
425 # see if it's in the reverse mapping (for hashable values) |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
426 try: |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
427 if value in cls._value2member_map_: |
|
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
428 return cls._value2member_map_[value] |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
429 except TypeError: |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
430 # not there, now do long search -- O(n) behavior |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
431 for member in cls._member_map_.values(): |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
432 if member.value == value: |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
433 return member |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
434 raise ValueError("%s is not a valid %s" % (value, cls.__name__)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
435 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
436 def __repr__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
437 return "<%s.%s: %r>" % ( |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
438 self.__class__.__name__, self._name_, self._value_) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
439 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
440 def __str__(self): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
441 return "%s.%s" % (self.__class__.__name__, self._name_) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
442 |
|
85127
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
443 def __dir__(self): |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
444 added_behavior = [m for m in self.__class__.__dict__ if m[0] != '_'] |
|
85790
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
445 return (['__class__', '__doc__', '__module__', 'name', 'value'] + |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
446 added_behavior) |
|
85127
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
447 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
448 def __eq__(self, other): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
449 if type(other) is self.__class__: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
450 return self is other |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
451 return NotImplemented |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
452 |
|
85485
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
453 def __format__(self, format_spec): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
454 # mixed-in Enums should use the mixed-in type's __format__, otherwise |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
455 # we can get strange results with the Enum name showing up instead of |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
456 # the value |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
457 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
458 # pure Enum branch |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
459 if self._member_type_ is object: |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
460 cls = str |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
461 val = str(self) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
462 # mix-in branch |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
463 else: |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
464 cls = self._member_type_ |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
465 val = self.value |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
466 return cls.__format__(val, format_spec) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85222
diff
changeset
|
467 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
468 def __getnewargs__(self): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
469 return (self._value_, ) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
470 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
471 def __hash__(self): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
472 return hash(self._name_) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
473 |
|
85816
96081e7526f0
Issue19030: fixed comment that was still referring to a changed descriptor.
Ethan Furman <ethan@stoneleaf.us>
parents:
85796
diff
changeset
|
474 # DynamicClassAttribute is used to provide access to the `name` and |
|
96081e7526f0
Issue19030: fixed comment that was still referring to a changed descriptor.
Ethan Furman <ethan@stoneleaf.us>
parents:
85796
diff
changeset
|
475 # `value` properties of enum members while keeping some measure of |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
476 # protection from modification, while still allowing for an enumeration |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
477 # to have members named `name` and `value`. This works because enumeration |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
478 # members are not set directly on the enum class -- __getattr__ is |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
479 # used to look them up. |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
480 |
|
85796
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
481 @DynamicClassAttribute |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
482 def name(self): |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
483 """The name of the Enum member.""" |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
484 return self._name_ |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
485 |
|
85796
436b606ecfe8
Close #19030: improvements to inspect and Enum.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
486 @DynamicClassAttribute |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
487 def value(self): |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
488 """The value of the Enum member.""" |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
489 return self._value_ |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
490 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
491 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
492 class IntEnum(int, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
493 """Enum where members are also (and must be) ints""" |
|
84713
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
494 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
495 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
496 def unique(enumeration): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
497 """Class decorator for enumerations ensuring unique member values.""" |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
498 duplicates = [] |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
499 for name, member in enumeration.__members__.items(): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
500 if name != member.name: |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
501 duplicates.append((name, member.name)) |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
502 if duplicates: |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
503 alias_details = ', '.join( |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
504 ["%s -> %s" % (alias, name) for (alias, name) in duplicates]) |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
505 raise ValueError('duplicate values found in %r: %s' % |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
506 (enumeration, alias_details)) |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
507 return enumeration |
