Skip to content

IntFlag crashes with no single bit members #93035

Closed
@tyehle

Description

@tyehle

Bug report

IntFlag causes a crash on 3.11.0b1 when there are no single bit members.

Minimal example is

from enum import IntFlag
class E(IntFlag):
  X = 3

this results in

Traceback (most recent call last):
  File "poc.py", line 2, in <module>
    class E(IntFlag):
    ^^^^^^^^^^^^^^^^^^^
  File "3.11.0b1/lib/python3.11/enum.py", line 580, in __new__
    member = list(enum_class)[0]
             ~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

This is used in practice when values with only single bits set aren't valid, eg:

Expensive = 0b001
Cramped   = 0b010
Ugly      = 0b100

class HouseAttributes(IntFlag):
  Cheap = Cramped | Ugly
  Spacious = Expensive | Ugly
  Beautiful = Expensive | Cramped
  Standard = Expensive | Cramped | Ugly

Your environment

  • CPython versions tested on: 3.11.0b1
  • Operating system and architecture: Linux and Mac

Metadata

Metadata

Assignees

Labels

3.11only security fixes3.12only security fixesstdlibPython 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