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

Support indexing and iterating Enum type objects #741

Closed
JukkaL opened this issue Aug 10, 2015 · 13 comments
Closed

Support indexing and iterating Enum type objects #741

JukkaL opened this issue Aug 10, 2015 · 13 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 10, 2015

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
@johnthagen
Copy link
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__"

@gvanrossum gvanrossum added this to the 0.5 milestone Sep 1, 2016
@gvanrossum
Copy link
Member

See also #2305.

@choucavalier
Copy link

Is someone working on this?

@gvanrossum
Copy link
Member

Is someone working on this?

Not yet.

@gvanrossum gvanrossum removed this from the 0.5 milestone Mar 29, 2017
@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 4, 2017

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

@ilevkivskyi
Copy link
Member

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

for color in Color:
    print(color)

still fails however.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 4, 2017

Yes, I just noticed that myself.

@JukkaL JukkaL changed the title Support indexing Enum type objects Support indexing and iterating Enum type objects Apr 4, 2017
@gvanrossum
Copy link
Member

Iterating is now fix by python/typeshed#1136.

@choucavalier
Copy link

Thanks @gvanrossum! 😄

@jboning
Copy link
Contributor

jboning commented Apr 5, 2017

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.

@JelleZijlstra
Copy link
Member

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

@gvanrossum
Copy link
Member

Could you file a new issue?

@jboning
Copy link
Contributor

jboning commented Apr 6, 2017

Forked off another issue at #3137

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

No branches or pull requests

7 participants