Repository navigation
Inverted enum.Flag #107538
Copy link
Copy link
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
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 integerTraceback:
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 integerYour 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
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsDone
Activity
Eclips4 commentedon Aug 1, 2023
Cannot reproduce on current main. Seems that this case fixed by #105542
ethanfurman commentedon Aug 1, 2023
It's not failing, and the first three work correctly, but
Y.dshould be an alias forY.b, not-2.GrafLearnt commentedon Aug 1, 2023
@ethanfurman may be you are right but I find following quite confusing:
~achanged whilearemaining the sameethanfurman commentedon Aug 1, 2023
@GrafLearnt, in your latest example,
dshould be6(i.e.b|c). Thanks for the reminder, this will be a more involved fix than I was originally thinking.GrafLearnt commentedon Aug 1, 2023
@ethanfurman
d = b | cwill work perfectly fine until someone add new member without adding it todethanfurman commentedon Aug 1, 2023
Well, presumably, if they write
d = b|cthat's what they want; whiled = ~ameans every member that's nota.GrafLearnt commentedon Aug 1, 2023
@ethanfurman
I love it:
But this doesn't look correct to me:
Kindly ask to clarify if I am allowed to define inverted.
It brakes iteration of ~Y.a
Also I get following:
ethanfurman commentedon Aug 1, 2023
Defining
~aas a member is what is not working correctly.I believe that last error is fixed in 3.11.5.
GrafLearnt commentedon Aug 1, 2023
@ethanfurman is defining inverted restricted or will be fixed?
Works fine in Python 3.10.12:
ethanfurman commentedon Aug 1, 2023
Will be fixed.
gh-107538: [Enum] fix handling of inverted/negative values (GH-132273)
pythongh-107538: [Enum] fix handling of inverted/negative values (pyt…
pythongh-107538: [Enum] fix handling of inverted/negative values (pyt…
pythongh-107538: [Enum] fix handling of inverted/negative values (pyt…
pythongh-107538: [Enum] fix handling of inverted/negative values (pyt…
pythongh-107538: [Enum] fix handling of inverted/negative values (pyt…