This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: add __iter__ to enum.Flag members
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: Guy Gangemi, barry, cryvate, eli.bendersky, ethan.furman, hongweipeng, rhettinger
Priority: normal Keywords: patch

Created on 2017-12-05 03:44 by Guy Gangemi, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22221 closed ethan.furman, 2020-09-12 23:41
PR 23368 closed hongweipeng, 2020-11-18 14:46
Messages (5)
msg307629 - (view) Author: Guy Gangemi (Guy Gangemi) Date: 2017-12-05 03:44
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()

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
msg311204 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2018-01-30 00:29
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
msg377152 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-19 08:30
Problem:  What to do when the Flag has compound members?

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

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

  --> list(Color.WHITE)
  [Color.BLUE, Color.GREEN, Color.RED]
msg381368 - (view) Author: Henk-Jaap Wagenaar (cryvate) * Date: 2020-11-18 18:32
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.
msg385060 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-01-14 01:26
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.
Date User Action Args
2022-04-11 14:58:55adminsetgithub: 76399
2021-01-14 01:26:05ethan.furmansetstatus: open -> closed
resolution: fixed
messages: + msg385060

stage: patch review -> resolved
2020-11-18 18:32:21cryvatesetnosy: + cryvate
messages: + msg381368
2020-11-18 14:46:23hongweipengsetnosy: + hongweipeng

pull_requests: + pull_request22261
stage: needs patch -> patch review
2020-09-19 08:30:17ethan.furmansetstatus: closed -> open
resolution: fixed -> (no value)
messages: + msg377152

stage: resolved -> needs patch
2020-09-17 08:23:54ethan.furmansetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
components: + Library (Lib)
versions: + Python 3.10, - Python 3.8
2020-09-12 23:41:50ethan.furmansetkeywords: + patch
stage: patch review
pull_requests: + pull_request21276
2018-01-30 00:29:57ethan.furmansetversions: + Python 3.8
nosy: + barry, rhettinger, eli.bendersky

messages: + msg311204

stage: needs patch -> (no value)
2018-01-18 19:31:45ethan.furmansetassignee: ethan.furman
stage: needs patch
2017-12-05 05:05:41berker.peksagsetnosy: + ethan.furman
2017-12-05 03:44:12Guy Gangemicreate