Skip to content

Inverted enum.Flag #107538

Closed
Closed
@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

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

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy