diff options
| author | Ethan Furman <ethan@stoneleaf.us> | 2016-01-15 15:01:33 -0800 |
|---|---|---|
| committer | Ethan Furman <ethan@stoneleaf.us> | 2016-01-15 15:01:33 -0800 |
| commit | 60255b67b986d4c448153bef16755599afbfdaa2 (patch) | |
| tree | 40835fb60bb87fcbbab7418e5230eeaa64d47f46 /Lib | |
| parent | 43cfd8240c274b2f4cf0588f3defae83e7379a55 (diff) | |
| download | cpython-git-60255b67b986d4c448153bef16755599afbfdaa2.tar.gz | |
revert change 87a9dff5106c: pure Enum members again evaluate to True;
update Finer Points section of docs to cover boolean evaluation;
add more tests for pure and mixed boolean evaluation
Diffstat (limited to 'Lib')
| -rw-r--r-- | Lib/enum.py | 3 | ||||
| -rw-r--r-- | Lib/test/test_enum.py | 17 |
2 files changed, 16 insertions, 4 deletions
diff --git a/Lib/enum.py b/Lib/enum.py index 35a9c77935..ac89d6ba26 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -482,9 +482,6 @@ 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 e4e6c2b51a..7985948041 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -272,11 +272,26 @@ class TestEnum(unittest.TestCase): _any_name_ = 9 def test_bool(self): + # plain Enum members are always True class Logic(Enum): true = True false = False self.assertTrue(Logic.true) - self.assertFalse(Logic.false) + self.assertTrue(Logic.false) + # unless overridden + class RealLogic(Enum): + true = True + false = False + def __bool__(self): + return bool(self._value_) + self.assertTrue(RealLogic.true) + self.assertFalse(RealLogic.false) + # mixed Enums depend on mixed-in type + class IntLogic(int, Enum): + true = 1 + false = 0 + self.assertTrue(IntLogic.true) + self.assertFalse(IntLogic.false) def test_contains(self): Season = self.Season |
