Open
Description
Bug report
Checklist
- I am confident this is a bug in CPython, not a bug in a third-party project
- I have searched the CPython issue tracker, and am confident this bug has not been reported before
A clear and concise description of the bug
Adding inverted option to enum.Flag brakes logic of enum.
I assume it caused by ambiguous implementation of bitwise NOT
in Flag which processes ~value option different inside class scope and outside class scope unlike bitwise OR
import enum
class X(enum.Flag):
a = enum.auto()
b = enum.auto()
c = a | b
assert list(X) == [X.a, X.b]
assert ~X.a == X.b
assert list(~X.a) == [X.b]
class Y(enum.Flag):
a = enum.auto()
b = enum.auto()
c = a | b
d = ~a # this line brakes the code
assert list(Y) == [Y.a, Y.b]
assert ~Y.a == Y.b # AssertionError
assert list(~Y.a) == [Y.b] # ValueError: -2 is not a positive integer
Traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/enum.py", line 1482, in __iter__
yield from self._iter_member_(self._value_)
File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/enum.py", line 1359, in _iter_member_by_value_
for val in _iter_bits_lsb(value & cls._flag_mask_):
File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/enum.py", line 122, in _iter_bits_lsb
raise ValueError('%r is not a positive integer' % original)
ValueError: -2 is not a positive integer
Your environment
- CPython versions tested on: Clang 14.0.3 (clang-1403.0.22.14.1)
- Operating system and architecture: 22.5.0 Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000 arm64