[Python-checkins] cpython (merge 3.4 -> 3.5): Issue26748: Enum classes should evaluate as True

ethan.furman python-checkins at python.org
Thu Apr 14 03:04:12 EDT 2016


https://hg.python.org/cpython/rev/f840608f79da
changeset:   100963:f840608f79da
branch:      3.5
parent:      100958:450f36750cb9
parent:      100962:772805538caf
user:        Ethan Furman <ethan at stoneleaf.us>
date:        Wed Apr 13 23:53:45 2016 -0700
summary:
  Issue26748: Enum classes should evaluate as True

files:
  Lib/enum.py           |   6 ++++++
  Lib/test/test_enum.py |  13 +++++++++++++
  2 files changed, 19 insertions(+), 0 deletions(-)


diff --git a/Lib/enum.py b/Lib/enum.py
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -206,6 +206,12 @@
             enum_class.__new__ = Enum.__new__
         return enum_class
 
+    def __bool__(self):
+        """
+        classes/types should always be True.
+        """
+        return True
+
     def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1):
         """Either returns an existing member, or creates a new enum class.
 
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
@@ -253,6 +253,19 @@
         with self.assertRaises(AttributeError):
             del Season.SPRING.name
 
+    def test_bool_of_class(self):
+        class Empty(Enum):
+            pass
+        self.assertTrue(bool(Empty))
+
+    def test_bool_of_member(self):
+        class Count(Enum):
+            zero = 0
+            one = 1
+            two = 2
+        for member in Count:
+            self.assertTrue(bool(member))
+
     def test_invalid_names(self):
         with self.assertRaises(ValueError):
             class Wrong(Enum):

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list