Skip to content

mypy interprets variable as a type during Enum indexing #1381

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

Closed
cduong opened this issue Apr 14, 2016 · 4 comments
Closed

mypy interprets variable as a type during Enum indexing #1381

cduong opened this issue Apr 14, 2016 · 4 comments
Assignees
Labels
bug mypy got something wrong priority-1-normal

Comments

@cduong
Copy link

cduong commented Apr 14, 2016

Example code, where row.status is expected to be a string:

class Status(Enum):
    SUCCEEDED = 1
    FAILED = 2

def get_status(row):
    # type: (Any) -> Status
    status = Status[row.status]
    return status

Error:

note: In function "test":
error: Invalid type "row"

In a similar case, I also received this error in addition to the above: error: Generic type not valid as an expression any more (use '# type:' comment instead)

@cduong cduong changed the title mypy interprets Enum indexing using variable as a type mypy interprets variable as a type during Enum indexing Apr 14, 2016
@gvanrossum
Copy link
Member

gvanrossum commented Apr 14, 2016 via email

@JukkaL JukkaL added the feature label Apr 15, 2016
@gvanrossum gvanrossum added this to the 0.5 milestone Apr 28, 2016
@gvanrossum gvanrossum added bug mypy got something wrong and removed feature labels Apr 28, 2016
@Stiivi
Copy link

Stiivi commented Feb 1, 2017

Different kind of error is reported when more complex indexing argument is passed:

class Choice(Enum):           
    foo = 0                   
    bar = 1                   
                              
def from_value(value: str) -> Choice: 
    return Choice[value.lower()] 

Error: Type expected within [...]

@gvanrossum
Copy link
Member

I think maybe this also ought to be given higher priority.

@gvanrossum gvanrossum self-assigned this Feb 4, 2017
@gvanrossum
Copy link
Member

FWIW I am going special-case enums here, rather than change the behavior for all non-generic classes. (Generic enums make no sense, but we don't currently detect this.)

gvanrossum pushed a commit that referenced this issue Feb 6, 2017
JukkaL pushed a commit that referenced this issue Feb 7, 2017
* Allow E[<str>] where E is an Enum type.

Fixes #1381.

* Respond to review by @ilevkivskyi
gvanrossum pushed a commit that referenced this issue Feb 12, 2017
…#2827)

Fix #1771. (Seems to be much simpler than I thought)

In particular, this will allow removing some special-casing for enum (#2812).

Related to #1381.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

No branches or pull requests

4 participants