Skip to content

Inverted enum.Flag#107538

@GrafLearnt

Description

@GrafLearnt

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

importenumclassX(enum.Flag): a=enum.auto() b=enum.auto() c=a|bassertlist(X) == [X.a, X.b] assert~X.a==X.bassertlist(~X.a) == [X.b] classY(enum.Flag): a=enum.auto() b=enum.auto() c=a|bd=~a# this line brakes the codeassertlist(Y) == [Y.a, Y.b] assert~Y.a==Y.b# AssertionErrorassertlist(~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/[email protected]/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/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/enum.py", line 1359, in _iter_member_by_value_ forvalin _iter_bits_lsb(value & cls._flag_mask_): File "/opt/homebrew/Cellar/[email protected]/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

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions