Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add __iter__ to enum.Flag members #76399

Closed
GuyGangemi mannequin opened this issue Dec 5, 2017 · 5 comments
Closed

add __iter__ to enum.Flag members #76399

GuyGangemi mannequin opened this issue Dec 5, 2017 · 5 comments
Assignees
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@GuyGangemi
Copy link
Mannequin

GuyGangemi mannequin commented Dec 5, 2017

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']

    @GuyGangemi
    Copy link
    Mannequin Author

    GuyGangemi mannequin commented Dec 5, 2017

    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

    @GuyGangemi GuyGangemi mannequin added the type-feature A feature request or enhancement label Dec 5, 2017
    @ethanfurman ethanfurman self-assigned this Jan 18, 2018
    @ethanfurman
    Copy link
    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

    @ethanfurman ethanfurman added the 3.8 only security fixes label Jan 30, 2018
    @ethanfurman ethanfurman added 3.10 only security fixes stdlib Python modules in the Lib dir and removed 3.8 only security fixes labels Sep 17, 2020
    @ethanfurman
    Copy link
    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]

    @ethanfurman ethanfurman reopened this Sep 19, 2020
    @Cryvate
    Copy link
    Mannequin

    Cryvate mannequin commented Nov 18, 2020

    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.

    @ethanfurman
    Copy link
    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant