cpython: f33fc2117bb2 (original) (raw)

Mercurial > cpython

changeset 103011:f33fc2117bb2

issue23591: bool(empty_flags) == False; more docs & tests [#23591]

Ethan Furman ethan@stoneleaf.us
date Fri, 02 Sep 2016 16:32:32 -0700
parents 31586a2f01b6
children 3ff76a59eb6b
files Doc/library/enum.rst Lib/enum.py Lib/test/test_enum.py
diffstat 3 files changed, 78 insertions(+), 3 deletions(-)[+] [-] Doc/library/enum.rst 62 Lib/enum.py 3 Lib/test/test_enum.py 16

line wrap: on

line diff

--- a/Doc/library/enum.rst +++ b/Doc/library/enum.rst @@ -546,6 +546,10 @@ members also subclass :class:int and c Any operation on an :class:IntFlag member besides the bit-wise operations will lose the :class:IntFlag membership. +.. versionadded:: 3.6 + +Sample :class:IntFlag class:: + >>> from enum import IntFlag >>> class Perm(IntFlag): ... R = 4 @@ -560,19 +564,71 @@ will lose the :class:IntFlag membershi >>> Perm.R in RW True -.. versionadded:: 3.6 +It is also possible to name the combinations:: +

+ +Another important difference between :class:IntFlag and :class:Enum is that +if no flags are set (the value is 0), its boolean evaluation is :data:False:: +

+ +Because :class:IntFlag members are also subclasses of :class:int they can +be combined with them:: +

Flag ^^^^ The last variation is :class:Flag. Like :class:IntFlag, :class:Flag -members can be combined using the bitwise operators (^, |, ^, ~). Unlike +members can be combined using the bitwise operators (&, |, ^, ~). Unlike :class:IntFlag, they cannot be combined with, nor compared against, any -other :class:Flag enumeration nor :class:int. +other :class:Flag enumeration, nor :class:int. .. versionadded:: 3.6 +Like :class:IntFlag, if a combination of :class:Flag members results in no +flags being set, the boolean evaluation is :data:False:: +

+ +Giving a name to the "no flags set" condition does not change its boolean +value:: +

+ .. note:: For the majority of new code, :class:Enum and :class:Flag are strongly

--- a/Lib/enum.py +++ b/Lib/enum.py @@ -714,6 +714,9 @@ class Flag(Enum): '|'.join([str(m.name or m.value) for m in members]), )

+ def or(self, other): if not isinstance(other, self.class): return NotImplemented

--- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -1767,6 +1767,14 @@ class TestFlag(unittest.TestCase): self.assertIs(Open.WO & ~Open.WO, Open.RO) self.assertIs((Open.WO|Open.CE) & ~Open.WO, Open.CE)

+ def test_programatic_function_string(self): Perm = Flag('Perm', 'R W X') lst = list(Perm) @@ -2137,6 +2145,14 @@ class TestIntFlag(unittest.TestCase): self.assertFalse(W in RX) self.assertFalse(X in RW)

+ class TestUnique(unittest.TestCase): def test_unique_clean(self):