Skip to content

mypy interprets variable as a type during Enum indexing #1381

Closed

Description

@cduong

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)

Activity

  1. changed the title [-]mypy interprets Enum indexing using variable as a type[/-] [+]mypy interprets variable as a type during Enum indexing[/+] on Apr 14, 2016
  2. gvanrossum commented on Apr 14, 2016

    @gvanrossum
    Member
  3. added this to the milestone on Apr 28, 2016
  4. added
    bugmypy got something wrong
    and removed on Apr 28, 2016
  5. Stiivi commented on Feb 1, 2017

    @Stiivi

    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 [...]

  6. gvanrossum commented on Feb 1, 2017

    @gvanrossum
    Member

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

  7. self-assigned this
    on Feb 4, 2017
  8. gvanrossum commented on Feb 6, 2017

    @gvanrossum
    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.)

  9. added a commit that references this issue on Feb 6, 2017
    5543b5a
  10. added a commit that references this issue on Feb 7, 2017
    b4492b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions