13

Why does Mypy complain that it requires a type annotation for a list comprehension variable, when it is impossible to annotate such a variable using MyPy?

Specifically, how can I resolve the following error:

from enum import EnumMeta

def spam( y: EnumMeta ):
    return [[x.value] for x in y] 🠜 Mypy: Need type annotation for 'x' 

cast doesn't work:

return [[cast(Enum, x).value] for x in y] 🠜 Mypy: Need type annotation for 'x'  

Even though Mypy doesn't support annotations (x:Enum) in such a case I see the usage of the variable can be annotated using cast (see this post). However, cast(Enum, x) doesn't stop Mypy complaining that the variable isn't annotated in the first place.

#type: doesn't work:

return [[x.value] for x in y] # type: Enum 🠜 Mypy: Misplaced type annotation

I also see that a for loop variable can be annotated using a comment, # type: (see this post). However, # type: Enum doesn't work for list comprehension's for.

0

2 Answers 2

23

In a list comprehension, the iterator must be cast instead of the elements.

from typing import Iterable, cast
from enum import EnumMeta, Enum

def spam(y: EnumMeta):
    return [[x.value] for x in cast(Iterable[Enum], y)]

This allows mypy to infer the type of x as well. In addition, at runtime it performs only 1 cast instead of n casts.

If spam can digest any iterable that produces enums, it is easier to type hint this directly.

from typing import Iterable
from enum import Enum

def spam(y: Iterable[Enum]):
    return [[x.value] for x in y]
6

MisterMyagi's answer is the best solution for this specific case. It may be worth noting, however, that it is also possible to declare the type of a variable before that variable is defined. The following also passes MyPy:

from enum import Enum

def spam(y: type[Enum]):
    x: Enum
    return [[x.value] for x in y]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.