From: Ethan Furman Date: Thu, 14 Apr 2016 06:52:09 +0000 (-0700) Subject: Issue26748: Enum classes should evaluate as True X-Git-Tag: v3.4.5rc1~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5de67b106ac980d9fe2bbe2a40cac8265b069c55;p=python Issue26748: Enum classes should evaluate as True --- diff --git a/Lib/enum.py b/Lib/enum.py index 3cd3df8428..7cb9d45b32 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -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. diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 5db40403f3..94336380e0 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -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):