]> granicus.if.org Git - python/commitdiff
Issue26748: Enum classes should evaluate as True
authorEthan Furman <ethan@stoneleaf.us>
Thu, 14 Apr 2016 06:52:09 +0000 (23:52 -0700)
committerEthan Furman <ethan@stoneleaf.us>
Thu, 14 Apr 2016 06:52:09 +0000 (23:52 -0700)
Lib/enum.py
Lib/test/test_enum.py

index 3cd3df8428445d708ec6cd22b68c7e2bd2aeaffc..7cb9d45b32b6052999f4b5ca1813491edb5f7f29 100644 (file)
@@ -193,6 +193,12 @@ class EnumMeta(type):
             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):
         """Either returns an existing member, or creates a new enum class.
 
index 5db40403f3931e141f9805488697baeb19a321e8..94336380e0d66fc16b0b17dabef96524441179e7 100644 (file)
@@ -257,6 +257,19 @@ class TestEnum(unittest.TestCase):
         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):