summaryrefslogtreecommitdiffstats
path: root/Lib/enum.py
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2013-09-22 23:18:19 (GMT)
committerEthan Furman <ethan@stoneleaf.us>2013-09-22 23:18:19 (GMT)
commit64a9972b40b8258a6e37498da6279f4e40a88e2d (patch)
treed87e479ab1d2dc8b8ea6f9603e614ce5d849fb98 /Lib/enum.py
parent0c47f34385e4b8f2136b372abd1d1994f6521473 (diff)
downloadcpython-64a9972b40b8258a6e37498da6279f4e40a88e2d.zip
cpython-64a9972b40b8258a6e37498da6279f4e40a88e2d.tar.gz
cpython-64a9972b40b8258a6e37498da6279f4e40a88e2d.tar.bz2
Close #19025: Better error message when trying to delete an Enum member.
Also slight code reorg for PEP 8 guidelines.
Diffstat (limited to 'Lib/enum.py')
-rw-r--r--Lib/enum.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index 86f94d9..0a7afc4 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -231,8 +231,17 @@ class EnumMeta(type):
def __contains__(cls, member):
return isinstance(member, cls) and member.name in cls._member_map_
+ def __delattr__(cls, attr):
+ # nicer error message when someone tries to delete an attribute
+ # (see issue19025).
+ if attr in cls._member_map_:
+ raise AttributeError(
+ "%s: cannot delete Enum member." % cls.__name__)
+ super().__delattr__(attr)
+
def __dir__(self):
- return ['__class__', '__doc__', '__members__', '__module__'] + self._member_names_
+ return (['__class__', '__doc__', '__members__', '__module__'] +
+ self._member_names_)
def __getattr__(cls, name):
"""Return the enum member matching `name`
@@ -459,7 +468,8 @@ class Enum(metaclass=EnumMeta):
def __dir__(self):
added_behavior = [m for m in self.__class__.__dict__ if m[0] != '_']
- return ['__class__', '__doc__', '__module__', 'name', 'value'] + added_behavior
+ return (['__class__', '__doc__', '__module__', 'name', 'value'] +
+ added_behavior)
def __eq__(self, other):
if type(other) is self.__class__: