[Python-checkins] cpython (merge default -> default): branch merge
ethan.furman
python-checkins at python.org
Fri Jan 15 18:03:23 EST 2016
https://hg.python.org/cpython/rev/4b1bca0b560f
changeset: 99912:4b1bca0b560f
parent: 99910:6908b2c9a404
parent: 99911:52dc28ee3c88
user: Ethan Furman <ethan at stoneleaf.us>
date: Fri Jan 15 15:03:12 2016 -0800
summary:
branch merge
files:
Doc/library/enum.rst | 11 ++++++++++-
Lib/enum.py | 3 ---
Lib/test/test_enum.py | 17 ++++++++++++++++-
3 files changed, 26 insertions(+), 5 deletions(-)
diff --git a/Doc/library/enum.rst b/Doc/library/enum.rst
--- a/Doc/library/enum.rst
+++ b/Doc/library/enum.rst
@@ -257,7 +257,7 @@
>>> Color.red < Color.blue
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
- TypeError: '<' not supported between instances of 'Color' and 'Color'
+ TypeError: unorderable types: Color() < Color()
Equality comparisons are defined though::
@@ -747,6 +747,15 @@
.. versionchanged:: 3.5
+Boolean evaluation: Enum classes that are mixed with non-Enum types (such as
+:class:`int`, :class:`str`, etc.) are evaluated according to the mixed-in
+type's rules; otherwise, all members evaluate as ``True``. To make your own
+Enum's boolean evaluation depend on the member's value add the following to
+your class::
+
+ def __bool__(self):
+ return bool(self._value_)
+
The :attr:`__members__` attribute is only available on the class.
If you give your :class:`Enum` subclass extra methods, like the `Planet`_
diff --git a/Lib/enum.py b/Lib/enum.py
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -482,9 +482,6 @@
def __str__(self):
return "%s.%s" % (self.__class__.__name__, self._name_)
- def __bool__(self):
- return bool(self._value_)
-
def __dir__(self):
added_behavior = [
m
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -272,11 +272,26 @@
_any_name_ = 9
def test_bool(self):
+ # plain Enum members are always True
class Logic(Enum):
true = True
false = False
self.assertTrue(Logic.true)
- self.assertFalse(Logic.false)
+ self.assertTrue(Logic.false)
+ # unless overridden
+ class RealLogic(Enum):
+ true = True
+ false = False
+ def __bool__(self):
+ return bool(self._value_)
+ self.assertTrue(RealLogic.true)
+ self.assertFalse(RealLogic.false)
+ # mixed Enums depend on mixed-in type
+ class IntLogic(int, Enum):
+ true = 1
+ false = 0
+ self.assertTrue(IntLogic.true)
+ self.assertFalse(IntLogic.false)
def test_contains(self):
Season = self.Season
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list