From: Ethan Furman Date: Fri, 18 Sep 2015 04:49:12 +0000 (-0700) Subject: Close issue24840: Enum._value_ is queried for bool(); original patch by Mike Lundy X-Git-Tag: v3.6.0a1~1537 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6db1fd5fb8b2287cd4713c95f0df451e375c8853;p=python Close issue24840: Enum._value_ is queried for bool(); original patch by Mike Lundy --- diff --git a/Lib/enum.py b/Lib/enum.py index c28f3452a7..616b2eac15 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -476,6 +476,9 @@ class Enum(metaclass=EnumMeta): 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 index 4b5d0d07bc..0f7b769b7a 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -270,6 +270,13 @@ class TestEnum(unittest.TestCase): class Wrong(Enum): _any_name_ = 9 + def test_bool(self): + class Logic(Enum): + true = True + false = False + self.assertTrue(Logic.true) + self.assertFalse(Logic.false) + def test_contains(self): Season = self.Season self.assertIn(Season.AUTUMN, Season) diff --git a/Misc/ACKS b/Misc/ACKS index e26be932e3..43b4c99779 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -877,6 +877,7 @@ Kang-Hao (Kenny) Lu Lukas Lueg Loren Luke Fredrik Lundh +Mike Lundy Zhongyue Luo Mark Lutz Taras Lyapun