]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-131045: [Enum] fix flag containment checks when using values (GH-131053...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 23 Mar 2025 16:51:26 +0000 (17:51 +0100)
committerGitHub <noreply@github.com>
Sun, 23 Mar 2025 16:51:26 +0000 (17:51 +0100)
gh-131045: [Enum] fix flag containment checks when using values (GH-131053)

Check would fail if value would create a pseudo-member, but that member
had not yet been created.  We now attempt to create a pseudo-member for
a passed-in value first.
(cherry picked from commit 17d06aeb5476099bc1acd89cd6f69e239e0f9350)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
Lib/enum.py
Lib/test/test_enum.py
Misc/NEWS.d/next/Library/2025-03-10-12-26-56.gh-issue-131045.s1TssJ.rst [new file with mode: 0644]

index 37f16976bbacde78ef02b8dd87919c9b5e16937b..9cab804115e484ce7a1624efd719afbaeb8a64cf 100644 (file)
@@ -746,12 +746,14 @@ class EnumType(type):
         `value` is in `cls` if:
         1) `value` is a member of `cls`, or
         2) `value` is the value of one of the `cls`'s members.
+        3) `value` is a pseudo-member (flags)
         """
         if isinstance(value, cls):
             return True
         try:
-            return value in cls._value2member_map_
-        except TypeError:
+            cls(value)
+            return True
+        except ValueError:
             return (
                     value in cls._unhashable_values_    # both structures are lists
                     or value in cls._hashable_values_
index 11e95d5b88b8c930f8678a908ee7f23b38fbf2c4..a2eb81c1da5589521a19636bb6ec12034094fef3 100644 (file)
@@ -463,6 +463,7 @@ class _EnumTests:
             self.assertEqual(str(TE), "<flag 'MainEnum'>")
             self.assertEqual(format(TE), "<flag 'MainEnum'>")
             self.assertTrue(TE(5) is self.dupe2)
+            self.assertTrue(7 in TE)
         else:
             self.assertEqual(repr(TE), "<enum 'MainEnum'>")
             self.assertEqual(str(TE), "<enum 'MainEnum'>")
@@ -4968,6 +4969,7 @@ class Color(enum.Enum)
  |      `value` is in `cls` if:
  |      1) `value` is a member of `cls`, or
  |      2) `value` is the value of one of the `cls`'s members.
+ |      3) `value` is a pseudo-member (flags)
  |
  |  __getitem__(name)
  |      Return the member matching `name`.
diff --git a/Misc/NEWS.d/next/Library/2025-03-10-12-26-56.gh-issue-131045.s1TssJ.rst b/Misc/NEWS.d/next/Library/2025-03-10-12-26-56.gh-issue-131045.s1TssJ.rst
new file mode 100644 (file)
index 0000000..b6aa072
--- /dev/null
@@ -0,0 +1 @@
+Fix issue with ``__contains__``, values, and pseudo-members for :class:`enum.Flag`.