Skip to content

add __iter__ to enum.Flag members #76399

Description

@GuyGangemi
mannequin
BPO 32218
Nosy @warsaw, @rhettinger, @ethanfurman, @hongweipeng, @cryvate
PRs
  • bpo-32218: make Flag and IntFlag members iterable #22221
  • bpo-32218: Only keep the one-bit flag in Flag.__iter__ #23368
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ethanfurman'
    closed_at = <Date 2021-01-14.01:26:05.907>
    created_at = <Date 2017-12-05.03:44:12.300>
    labels = ['type-feature', 'library', '3.10']
    title = 'add __iter__ to enum.Flag members'
    updated_at = <Date 2021-01-14.01:26:05.905>
    user = 'https://bugs.python.org/GuyGangemi'

    bugs.python.org fields:

    activity = <Date 2021-01-14.01:26:05.905>
    actor = 'ethan.furman'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2021-01-14.01:26:05.907>
    closer = 'ethan.furman'
    components = ['Library (Lib)']
    creation = <Date 2017-12-05.03:44:12.300>
    creator = 'Guy Gangemi'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32218
    keywords = ['patch']
    message_count = 5.0
    messages = ['307629', '311204', '377152', '381368', '385060']
    nosy_count = 7.0
    nosy_names = ['barry', 'rhettinger', 'eli.bendersky', 'ethan.furman', 'hongweipeng', 'cryvate', 'Guy Gangemi']
    pr_nums = ['22221', '23368']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue32218'
    versions = ['Python 3.10']

    Activity

    1. GuyGangemi commented on Dec 5, 2017

      GuyGangemimannequin
      MannequinAuthor

      I'm proposing to extend enum.Flag member functionality so it is iterable in a manner similar to enum.Flag subclasses.

      from enum import Flag, auto
      
      
      class FlagIter(Flag):
          def __iter__(self):
              for memeber in self._member_map_.values():
                  if member in self:
                      yield member
      
      
      class Colour(FlagIter):
          RED = auto()
          GREEN = auto()
          BLUE = auto()
          YELLOW = RED | GREEN
      
      cyan = Colour.GREEN | Colour.Blue
      
      print(*Colour)  # Colour.RED Colour.GREEN Colour.BLUE Colour.YELLOW
      
      # Without the enhancement, 'not iterable' is thrown for these
      print(*cyan)  # Colour.GREEN Colour.BLUE
      print(*Colour.YELLOW)  # Colour.RED Colour.GREEN Colour.YELLOW
      print(*~Colour.RED)  # Colour.GREEN Colour.BLUE
    2. self-assigned this
      on Jan 18, 2018
    3. ethanfurman commented on Jan 30, 2018

      @ethanfurman
      Member

      This functionality is now in the third-party aenum* library.

      Are there any strong use-cases for this behavior such that it should be in the stdlib?

      • as of version 2.0.10, available on PyPI, and I am its author
    4. added
      stdlibStandard Library Python modules in the Lib/ directory
      and removed on Sep 17, 2020
    5. ethanfurman commented on Sep 19, 2020

      @ethanfurman
      Member

      Problem: What to do when the Flag has compound members?

          class Color(Flag):
              BLACK = 0
              RED = 1
              GREEN = 2
              BLUE = 4
              PURPLE = RED|BLUE
              WHITE = RED|GREEN|BLUE

      I think the answer is: only return the single members. So

      --> list(Color.WHITE)
      [Color.BLUE, Color.GREEN, Color.RED]

    6. Cryvate commented on Nov 18, 2020

      Cryvatemannequin
      Mannequin

      What does aenum do and has there been any feedback on it?

      To me I would see what you suggest as surprising but I don't use enums often (I should use them more!) so take that with a grain of salt, and also surprising != wrong/not good.

    7. ethanfurman commented on Jan 14, 2021

      @ethanfurman
      Member

      Final outcome:

      Flag has been redesigned such that any flag comprised of a single bit is canonical; flags comprised of multiple bits are considered aliases. During iteration only canonical flags are returned.

    8. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.10 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions