https://github.com/python/cpython/commit/b3dccc0ce2f53936fdae50498388679f2a408b3f commit: b3dccc0ce2f53936fdae50498388679f2a408b3f branch: 3.13 author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com> committer: picnixz <10796600+picn...@users.noreply.github.com> date: 2025-03-23T17:51:26+01:00 summary:
[3.13] gh-131045: [Enum] fix flag containment checks when using values (GH-131053) (#131167) 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 <et...@stoneleaf.us> Co-authored-by: Tomas R. <tomas.ro...@gmail.com> files: A Misc/NEWS.d/next/Library/2025-03-10-12-26-56.gh-issue-131045.s1TssJ.rst M Lib/enum.py M Lib/test/test_enum.py diff --git a/Lib/enum.py b/Lib/enum.py index 37f16976bbacde..9cab804115e484 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -746,12 +746,14 @@ def __contains__(cls, value): `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_ diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 11e95d5b88b8c9..a2eb81c1da5589 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -463,6 +463,7 @@ def test_basics(self): 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 index 00000000000000..b6aa07276bb546 --- /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`. _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com