Title: 3.11.4: ValueError when inverting enum.Flag member with mask member · Issue #105497 · python/cpython · GitHub
Open Graph Title: 3.11.4: ValueError when inverting enum.Flag member with mask member · Issue #105497 · python/cpython
X Title: 3.11.4: ValueError when inverting enum.Flag member with mask member · Issue #105497 · python/cpython
Description: With code such as: import enum class Flag(enum.Flag): A = 0x01 B = 0x02 Mask = 0xff print(~Flag.A) Python 3.10.11 prints Flag.B, and so does Python 3.11.3. However, with Python 3.11.4, this happens instead: Traceback (most recent call la...
Open Graph Description: With code such as: import enum class Flag(enum.Flag): A = 0x01 B = 0x02 Mask = 0xff print(~Flag.A) Python 3.10.11 prints Flag.B, and so does Python 3.11.3. However, with Python 3.11.4, this happens...
X Description: With code such as: import enum class Flag(enum.Flag): A = 0x01 B = 0x02 Mask = 0xff print(~Flag.A) Python 3.10.11 prints Flag.B, and so does Python 3.11.3. However, with Python 3.11.4, this happens...
Opengraph URL: https://github.com/python/cpython/issues/105497
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"3.11.4: ValueError when inverting enum.Flag member with mask member","articleBody":"With code such as:\r\n\r\n```python\r\nimport enum\r\n\r\nclass Flag(enum.Flag):\r\n A = 0x01\r\n B = 0x02\r\n Mask = 0xff\r\n\r\nprint(~Flag.A)\r\n```\r\n\r\nPython 3.10.11 prints `Flag.B`, and so does Python 3.11.3. However, with Python 3.11.4, this happens instead:\r\n\r\n```pytb\r\nTraceback (most recent call last):\r\n File \"/home/florian/tmp/f.py\", line 9, in \u003cmodule\u003e\r\n print(~Flag.A)\r\n ^^^^^^^\r\n File \"/usr/lib/python3.11/enum.py\", line 1542, in __invert__\r\n self._inverted_ = self.__class__(self._flag_mask_ ^ self._value_)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/lib/python3.11/enum.py\", line 711, in __call__\r\n return cls.__new__(cls, value)\r\n ^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/lib/python3.11/enum.py\", line 1136, in __new__\r\n raise exc\r\n File \"/usr/lib/python3.11/enum.py\", line 1113, in __new__\r\n result = cls._missing_(value)\r\n ^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/lib/python3.11/enum.py\", line 1454, in _missing_\r\n raise ValueError('%r: no members with value %r' % (cls, unknown))\r\nValueError: \u003cflag 'Flag'\u003e: no members with value 252\r\n```\r\n\r\nAs a workaround, a detour via `.value` works in this case:\r\n\r\n```python\r\n\u003e\u003e\u003e Flag((Flag.A | Flag.B).value \u0026 ~Flag.A.value)\r\n\u003cFlag.B: 2\u003e\r\n```\r\n\r\nThis causes issues with PyQt, which has the following flags ([as bindings from C++](https://github.com/qt/qtbase/blob/v6.5.1/src/corelib/global/qnamespace.h#L1047-L1057)):\r\n\r\n```python\r\n\u003e\u003e\u003e from PyQt6.QtCore import Qt\r\n\u003e\u003e\u003e for e in Qt.KeyboardModifier:\r\n... print(f\"{e.name} = {hex(e.value)}\")\r\n... \r\nNoModifier = 0x0\r\nShiftModifier = 0x2000000\r\nControlModifier = 0x4000000\r\nAltModifier = 0x8000000\r\nMetaModifier = 0x10000000\r\nKeypadModifier = 0x20000000\r\nGroupSwitchModifier = 0x40000000\r\nKeyboardModifierMask = 0xfe000000\r\n```\r\n\r\n(Output from Python 3.10 - with Python 3.11, `KeyboardModifierMask` goes missing in the output, and so does `Flag.Mask` above, but that seems like a different issue?)\r\n\r\nWith my project, I'm [trying to remove a modifier](https://github.com/qutebrowser/qutebrowser/issues/7735) from the given flags. With Python 3.10.11 and 3.11.3:\r\n\r\n```python\r\n\u003e\u003e\u003e (Qt.KeyboardModifier.ShiftModifier | Qt.KeyboardModifier.ControlModifier) \u0026 ~Qt.KeyboardModifier.ControlModifier\r\n\u003cKeyboardModifier.ShiftModifier: 33554432\u003e\r\n```\r\n\r\nBut with Python 3.11.4, same issue as above:\r\n\r\n```pytb\r\n\u003e\u003e\u003e from PyQt6.QtCore import Qt\r\n\u003e\u003e\u003e (Qt.KeyboardModifier.ShiftModifier | Qt.KeyboardModifier.ControlModifier) \u0026 ~Qt.KeyboardModifier.ControlModifier\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"/usr/lib/python3.11/enum.py\", line 1542, in __invert__\r\n self._inverted_ = self.__class__(self._flag_mask_ ^ self._value_)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/lib/python3.11/enum.py\", line 711, in __call__\r\n return cls.__new__(cls, value)\r\n ^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/lib/python3.11/enum.py\", line 1136, in __new__\r\n raise exc\r\n File \"/usr/lib/python3.11/enum.py\", line 1113, in __new__\r\n result = cls._missing_(value)\r\n ^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/lib/python3.11/enum.py\", line 1454, in _missing_\r\n raise ValueError('%r: no members with value %r' % (cls, unknown))\r\nValueError: \u003cflag 'KeyboardModifier'\u003e: no members with value 2147483648\r\n```\r\n\r\nAs a culprit, I suspect:\r\n\r\n- #103365\r\n- #103494\r\n- #103513\r\n\r\ncc @ethanfurman @benburrill\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-105542\n* gh-105571\n* gh-105572\n* gh-106468\n* gh-106620\n* gh-106621\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/The-Compiler","@type":"Person","name":"The-Compiler"},"datePublished":"2023-06-08T12:22:42.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":14},"url":"https://github.com/105497/cpython/issues/105497"}
| 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:eee7484a-b275-29f8-df92-ef453b157f2b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CC80:292113:6983AC:94867B:696A4C39 |
| html-safe-nonce | c0b169e8425fe5c6b7d0f95906529cf43080a7bb07b0df4cf0564444c283fb84 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQzgwOjI5MjExMzo2OTgzQUM6OTQ4NjdCOjY5NkE0QzM5IiwidmlzaXRvcl9pZCI6IjE4MjMwMTI4OTc5MTg5NjI3NDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d1f8c7a956b6cdee97e225db226f9c6b4cd50112b90fde4d473ec4fad4aeb363 |
| hovercard-subject-tag | issue:1747785896 |
| 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/105497/issue_layout |
| twitter:image | https://opengraph.githubassets.com/110f6ef9205921402f41300f273ff0f11b56acce91a0b9ca87358baf4341f011/python/cpython/issues/105497 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/110f6ef9205921402f41300f273ff0f11b56acce91a0b9ca87358baf4341f011/python/cpython/issues/105497 |
| og:image:alt | With code such as: import enum class Flag(enum.Flag): A = 0x01 B = 0x02 Mask = 0xff print(~Flag.A) Python 3.10.11 prints Flag.B, and so does Python 3.11.3. However, with Python 3.11.4, this happens... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | The-Compiler |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3f871c8e07f0ae1886fa8dac284166d28b09ad5bada6476fc10b674e489788ef |
| 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 | 63c426b30d262aba269ef14c40e3c817b384cd61 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width