From 8a9aee71268c77867d3cc96d43cbbdcbe8c0e1e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 24 Mar 2025 10:31:24 +0100 Subject: [PATCH] [3.12] gh-131045: [Enum] fix flag containment checks when using values (GH-131053) (#131232) * 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. --------- Co-authored-by: Ethan Furman Co-authored-by: Tomas R. --- Lib/enum.py | 7 ++++++- Lib/test/test_enum.py | 2 ++ .../Library/2025-03-10-12-26-56.gh-issue-131045.s1TssJ.rst | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-03-10-12-26-56.gh-issue-131045.s1TssJ.rst diff --git a/Lib/enum.py b/Lib/enum.py index eaa517e2fbc..c87aeef7157 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -771,10 +771,15 @@ 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 - return value in cls._value2member_map_ or value in cls._unhashable_values_ + try: + cls(value) + return True + except ValueError: + return value in cls._unhashable_values_ def __delattr__(cls, attr): # nicer error message when someone tries to delete an attribute diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 2e50ae0fe96..a606002ffe9 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -422,6 +422,7 @@ class _EnumTests: self.assertEqual(str(TE), "") self.assertEqual(format(TE), "") self.assertTrue(TE(5) is self.dupe2) + self.assertTrue(7 in TE) else: self.assertEqual(repr(TE), "") self.assertEqual(str(TE), "") @@ -4848,6 +4849,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 index 00000000000..b6aa07276bb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-10-12-26-56.gh-issue-131045.s1TssJ.rst @@ -0,0 +1 @@ +Fix issue with ``__contains__``, values, and pseudo-members for :class:`enum.Flag`.