You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mypy interprets variable as a type during Enum indexing #1381
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)
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
PEP 435 enums allow indexing the class to dynamically look up an enum. E.g.
`Color['green']` is the same as`Color.green`.
If the class is not generic mypy shouldn't flag it as an error.
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.)
Activity
[-]mypy interprets Enum indexing using variable as a type[/-][+]mypy interprets variable as a type during Enum indexing[/+]gvanrossum commentedon Apr 14, 2016
Stiivi commentedon Feb 1, 2017
Different kind of error is reported when more complex indexing argument is passed:
Error:
Type expected within [...]gvanrossum commentedon Feb 1, 2017
I think maybe this also ought to be given higher priority.
gvanrossum commentedon Feb 6, 2017
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.)
Allow E[<str>] where E is an Enum type.
Allow E[<str>] where E is an Enum type (#2812)
Allow using __getitem__ defined in metaclasses of non-generic classes (…