]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-120361: Add `nonmember` test with enum flags inside to `test_enum` (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 14 Jun 2024 17:59:12 +0000 (19:59 +0200)
committerGitHub <noreply@github.com>
Fri, 14 Jun 2024 17:59:12 +0000 (20:59 +0300)
gh-120361: Add `nonmember` test with enum flags inside to `test_enum` (GH-120364)

* gh-120361: Add `nonmember` test with enum flags inside to `test_enum`
(cherry picked from commit 7fadfd82ebf6ea90b38cb3f2a046a51f8601a205)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Doc/library/enum.rst
Lib/test/test_enum.py

index 10acff619f91e0f57c176cf4e130af0536060ae1..3d0747bf5c7faf15c850c8b9cb97f806a6c02b75 100644 (file)
@@ -517,7 +517,7 @@ Data Types
 
    ``Flag`` is the same as :class:`Enum`, but its members support the bitwise
    operators ``&`` (*AND*), ``|`` (*OR*), ``^`` (*XOR*), and ``~`` (*INVERT*);
-   the results of those operators are members of the enumeration.
+   the results of those operations are (aliases of) members of the enumeration.
 
    .. method:: __contains__(self, value)
 
index ccba0f91c868eb2d3109abc5070fccdecf0631a0..7e3952251c7898e85b2abb60a75c8bd8b0565a3e 100644 (file)
@@ -1455,6 +1455,27 @@ class TestSpecial(unittest.TestCase):
             spam = nonmember(SpamEnumIsInner)
         self.assertTrue(SpamEnum.spam is SpamEnumIsInner)
 
+    def test_using_members_as_nonmember(self):
+        class Example(Flag):
+            A = 1
+            B = 2
+            ALL = nonmember(A | B)
+
+        self.assertEqual(Example.A.value, 1)
+        self.assertEqual(Example.B.value, 2)
+        self.assertEqual(Example.ALL, 3)
+        self.assertIs(type(Example.ALL), int)
+
+        class Example(Flag):
+            A = auto()
+            B = auto()
+            ALL = nonmember(A | B)
+
+        self.assertEqual(Example.A.value, 1)
+        self.assertEqual(Example.B.value, 2)
+        self.assertEqual(Example.ALL, 3)
+        self.assertIs(type(Example.ALL), int)
+
     def test_nested_classes_in_enum_with_member(self):
         """Support locally-defined nested classes."""
         class Outer(Enum):