From 5de67b106ac980d9fe2bbe2a40cac8265b069c55 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Wed, 13 Apr 2016 23:52:09 -0700 Subject: Issue26748: Enum classes should evaluate as True --- Lib/enum.py | 6 ++++++ Lib/test/test_enum.py | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Lib/enum.py b/Lib/enum.py index 3cd3df8..7cb9d45 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 5db4040..9433638 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): -- cgit v0.12