]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-120361: Add `nonmember` test with enum flags inside to `test_enum` (GH-120364)
authorNikita Sobolev <mail@sobolevn.me>
Fri, 14 Jun 2024 17:25:35 +0000 (20:25 +0300)
committerGitHub <noreply@github.com>
Fri, 14 Jun 2024 17:25:35 +0000 (10:25 -0700)
* gh-120361: Add `nonmember` test with enum flags inside to `test_enum`

Doc/library/enum.rst
Lib/test/test_enum.py

index 8c604c2347a547907b0c1cc245070ce7c7aa5419..9cf94e342dad281c17bb31c3102d110d39bf84c1 100644 (file)
@@ -527,7 +527,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 529dfc62eff680a1b8bd173eef7569ba893642f5..99fd16ba361e6f9e4e2a963c5ea81dc8ef2aeadc 100644 (file)
@@ -1495,6 +1495,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):