Title: `enum.Flag.__contains__` changed behavior since python 3.12 · Issue #131045 · python/cpython · GitHub
Open Graph Title: `enum.Flag.__contains__` changed behavior since python 3.12 · Issue #131045 · python/cpython
X Title: `enum.Flag.__contains__` changed behavior since python 3.12 · Issue #131045 · python/cpython
Description: Bug report Bug description: I noticed some strange behavior with enum.Flag an the __contains__ method in Python 3.12/3.13, as shown in the following examples. Problem 1: Behavior changes at runtime In the following code snippet the first...
Open Graph Description: Bug report Bug description: I noticed some strange behavior with enum.Flag an the __contains__ method in Python 3.12/3.13, as shown in the following examples. Problem 1: Behavior changes at runtime...
X Description: Bug report Bug description: I noticed some strange behavior with enum.Flag an the __contains__ method in Python 3.12/3.13, as shown in the following examples. Problem 1: Behavior changes at runtime...
Opengraph URL: https://github.com/python/cpython/issues/131045
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`enum.Flag.__contains__` changed behavior since python 3.12","articleBody":"# Bug report\n\n### Bug description:\n\nI noticed some strange behavior with `enum.Flag` an the `__contains__` method in Python 3.12/3.13, as shown in the following examples.\n\n**Problem 1: Behavior changes at runtime**\nIn the following code snippet the first print statement returns `False`, which is expected, since `3` is not a member of `Weekday`. However, the second print statement returns `True`, which is unexpected, since `3` is still not a member of `Weekday`.\n\n```python\nimport enum\n\nclass Weekday(enum.Flag):\n MONDAY = 1\n TUESDAY = 2\n WEDNESDAY = 4\n THURSDAY = 8\n FRIDAY = 16\n SATURDAY = 32\n SUNDAY = 64\n\nprint(f\"{3 in Weekday}\") # =\u003e False (expected)\n_ = Weekday.MONDAY | Weekday.TUESDAY\nprint(f\"{3 in Weekday}\") # =\u003e True (not expected)\n```\n\n\n\n**Problem 2: Behavior is not comparable to Python 3.11**\nSince Python 3.12 the behavior of `Enum.__contains__` has changed, so that it is possible to compare not only with an enum-member, but also with non-enum-members (see [here](https://github.com/python/cpython/pull/93298) or [here](https://docs.python.org/3/library/enum.html#enum.EnumType.__contains__)). So with Python 3.11 the code above will raise an `TypeError: unsupported operand type(s) for 'in': 'int' and 'EnumType'`. There you have to change the code to the following. But this in turn always produces unexpected behavior in Python 3.12/3.13.\n\n```python\nimport enum\n\nclass Weekday(enum.Flag):\n MONDAY = 1\n TUESDAY = 2\n WEDNESDAY = 4\n THURSDAY = 8\n FRIDAY = 16\n SATURDAY = 32\n SUNDAY = 64\n\nprint(f\"{Weekday(3) in Weekday}\") # =\u003e Python 3.11: False (expected) Python 3.12/3.13: True (not expected)\n_ = Weekday.MONDAY | Weekday.TUESDAY\nprint(f\"{Weekday(3) in Weekday}\") # =\u003e Python 3.11: False (expected) Python 3.12/3.13: True (not expected)\n```\n\n**Conclusion**\nI would have expected that in all cases the result is `False`, but since Python 3.12 it gets very strange...\n\n### CPython versions tested on:\n\n3.12, 3.13\n\n### Operating systems tested on:\n\nWindows\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-131053\n* gh-131167\n* gh-131232\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/timrid","@type":"Person","name":"timrid"},"datePublished":"2025-03-10T14:29:22.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/131045/cpython/issues/131045"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bb03ff59-be2e-71c2-bf2c-790a60b7119e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A24C:2BBCB0:2099087:2B2F1A9:696B06F7 |
| html-safe-nonce | 551d612bc13f7f4abf51f07add5ec2bbfd9f18a750988cedfe641ba39c1631e7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMjRDOjJCQkNCMDoyMDk5MDg3OjJCMkYxQTk6Njk2QjA2RjciLCJ2aXNpdG9yX2lkIjoiMjE3NjkwNTA2NDgwNDQ1MjA4NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 00ef46ae7cdaa2849fbd22b5f1517c29a2766f409d1938d5a57cc3a3fcf07818 |
| hovercard-subject-tag | issue:2907471446 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/131045/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e2347c0791b96a65099895d920e4ac6168e2541a0da7a0fa9f74d63391035e51/python/cpython/issues/131045 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e2347c0791b96a65099895d920e4ac6168e2541a0da7a0fa9f74d63391035e51/python/cpython/issues/131045 |
| og:image:alt | Bug report Bug description: I noticed some strange behavior with enum.Flag an the __contains__ method in Python 3.12/3.13, as shown in the following examples. Problem 1: Behavior changes at runtime... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | timrid |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width