Skip to content

Support indexing and iterating Enum type objects #741

Closed

Description

@JukkaL

Assume this definition of Color:

from enum import Enum
class Color(Enum):
    red = 1
    green = 2
    blue = 3

Now this should be valid, but mypy complains about it:

Color['red']

The error message indicates that mypy thinks that the programmer is trying to construct a generic type, which is not the case here:

$ mypy t.py
t.py:8: error: Name 'red' is not defined

Activity

  1. johnthagen commented on Aug 27, 2016

    @johnthagen
    Contributor

    @JukkaL Another related Enum issue is that Mypy currently doesn't understand that Enum's can be iterated on.

    import enum
    
    
    @enum.unique
    class Color(enum.Enum):
        red = 1
        green = 2
        blue = 3
    
    for color in Color:
        print(color)

    When run:

    Color.red
    Color.green
    Color.blue
    

    When mypy 0.4.4 is run on the file:

    a.py:10: error: Iterable expected
    a.py:10: error: "Color" has no attribute "__iter__"
    
  2. added this to the milestone on Sep 1, 2016
  3. gvanrossum commented on Oct 24, 2016

    @gvanrossum
    Member

    See also #2305.

  4. choucavalier commented on Jan 20, 2017

    @choucavalier

    Is someone working on this?

  5. gvanrossum commented on Jan 20, 2017

    @gvanrossum
    Member

    Is someone working on this?

    Not yet.

  6. removed this from the milestone on Mar 29, 2017
  7. JukkaL commented on Apr 4, 2017

    @JukkaL
    CollaboratorAuthor

    Increasing priority since this seems to be among the most common issues mypy users encounter.

  8. ilevkivskyi commented on Apr 4, 2017

    @ilevkivskyi
    Member

    I just have tried Color['red'] and it works. The second snippet:

    for color in Color:
        print(color)

    still fails however.

  9. JukkaL commented on Apr 4, 2017

    @JukkaL
    CollaboratorAuthor

    Yes, I just noticed that myself.

  10. changed the title [-]Support indexing Enum type objects[/-] [+]Support indexing and iterating Enum type objects[/+] on Apr 4, 2017
  11. gvanrossum commented on Apr 4, 2017

    @gvanrossum
    Member

    Iterating is now fix by python/typeshed#1136.

  12. choucavalier commented on Apr 4, 2017

    @choucavalier

    Thanks @gvanrossum! 😄

  13. jboning commented on Apr 5, 2017

    @jboning
    Contributor

    I'm not sure this is completely fixed: Color['red'] works, but val = Color['red'] results in the old error.

    Adding an explicit annotation (val : Color = Color['red']) fixes the error and is an easy workaround, so whatever remains here is probably a lower priority to fix.

  14. JelleZijlstra commented on Apr 5, 2017

    @JelleZijlstra
    Member

    From the discussion on Gitter, it seems that val = Color['red'] is interpreted as constructing a type alias.

  15. gvanrossum commented on Apr 6, 2017

    @gvanrossum
    Member

    Could you file a new issue?

  16. jboning commented on Apr 6, 2017

    @jboning
    Contributor

    Forked off another issue at #3137

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions