Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25d94bb

Browse files
committed
issue23591: bool(empty_flags) == False; more docs & tests
1 parent 0443953 commit 25d94bb

3 files changed

Lines changed: 78 additions & 3 deletions

File tree

Doc/library/enum.rst

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ members also subclass :class:`int` and can be used wherever an :class:`int` is.
546546
Any operation on an :class:`IntFlag` member besides the bit-wise operations
547547
will lose the :class:`IntFlag` membership.
548548

549+
.. versionadded:: 3.6
550+
551+
Sample :class:`IntFlag` class::
552+
549553
>>> from enum import IntFlag
550554
>>> class Perm(IntFlag):
551555
... R = 4
@@ -560,19 +564,71 @@ will lose the :class:`IntFlag` membership.
560564
>>> Perm.R in RW
561565
True
562566

563-
.. versionadded:: 3.6
567+
It is also possible to name the combinations::
568+
569+
>>> class Perm(IntFlag):
570+
... R = 4
571+
... W = 2
572+
... X = 1
573+
... RWX = 7
574+
>>> Perm.RWX
575+
<Perm.RWX: 7>
576+
>>> ~Perm.RWX
577+
<Perm.0: 0>
578+
579+
Another important difference between :class:`IntFlag` and :class:`Enum` is that
580+
if no flags are set (the value is 0), its boolean evaluation is :data:`False`::
581+
582+
>>> Perm.R & Perm.X
583+
<Perm.0: 0>
584+
>>> bool(Perm.R & Perm.X)
585+
False
586+
587+
Because :class:`IntFlag` members are also subclasses of :class:`int` they can
588+
be combined with them::
589+
590+
>>> Perm.X | 8
591+
<Perm.8|X: 9>
564592

565593

566594
Flag
567595
^^^^
568596

569597
The last variation is :class:`Flag`. Like :class:`IntFlag`, :class:`Flag`
570-
members can be combined using the bitwise operators (^, \|, ^, ~). Unlike
598+
members can be combined using the bitwise operators (&, \|, ^, ~). Unlike
571599
:class:`IntFlag`, they cannot be combined with, nor compared against, any
572-
other :class:`Flag` enumeration nor :class:`int`.
600+
other :class:`Flag` enumeration, nor :class:`int`.
573601

574602
.. versionadded:: 3.6
575603

604+
Like :class:`IntFlag`, if a combination of :class:`Flag` members results in no
605+
flags being set, the boolean evaluation is :data:`False`::
606+
607+
>>> from enum import Flag
608+
>>> class Color(Flag):
609+
... red = 1
610+
... blue = 2
611+
... green = 4
612+
...
613+
>>> Color.red & Color.green
614+
<Color.0: 0>
615+
>>> bool(Color.red & Color.green)
616+
False
617+
618+
Giving a name to the "no flags set" condition does not change its boolean
619+
value::
620+
621+
>>> class Color(Flag):
622+
... black = 0
623+
... red = 1
624+
... blue = 2
625+
... green = 4
626+
...
627+
>>> Color.black
628+
<Color.black: 0>
629+
>>> bool(Color.black)
630+
False
631+
576632
.. note::
577633

578634
For the majority of new code, :class:`Enum` and :class:`Flag` are strongly

Lib/enum.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,9 @@ def __str__(self):
714714
'|'.join([str(m._name_ or m._value_) for m in members]),
715715
)
716716

717+
def __bool__(self):
718+
return bool(self._value_)
719+
717720
def __or__(self, other):
718721
if not isinstance(other, self.__class__):
719722
return NotImplemented

Lib/test/test_enum.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,14 @@ def test_invert(self):
17671767
self.assertIs(Open.WO & ~Open.WO, Open.RO)
17681768
self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE)
17691769

1770+
def test_bool(self):
1771+
Perm = self.Perm
1772+
for f in Perm:
1773+
self.assertTrue(f)
1774+
Open = self.Open
1775+
for f in Open:
1776+
self.assertEqual(bool(f.value), bool(f))
1777+
17701778
def test_programatic_function_string(self):
17711779
Perm = Flag('Perm', 'R W X')
17721780
lst = list(Perm)
@@ -2137,6 +2145,14 @@ def test_containment(self):
21372145
self.assertFalse(W in RX)
21382146
self.assertFalse(X in RW)
21392147

2148+
def test_bool(self):
2149+
Perm = self.Perm
2150+
for f in Perm:
2151+
self.assertTrue(f)
2152+
Open = self.Open
2153+
for f in Open:
2154+
self.assertEqual(bool(f.value), bool(f))
2155+
21402156
class TestUnique(unittest.TestCase):
21412157

21422158
def test_unique_clean(self):

0 commit comments

Comments
 (0)