Let's say I have a function that can only take a list of strings, like the following:
from typing import List
def iter_lower(lst: List[str]) -> List[str]:
new_lst = []
for string in lst:
new_lst.append(string.lower())
return new_lst
lst: List = ["Hello", "World", 1]
iter_lower(lst)
mypy
does not complain about this, even though it is clearly wrong since lst
is a generic list, containing an integer which is incompatible with .lower()
. Is there some hint I could use in the argument list of iter_lower
that will cause mypy
to reject this usage?
The problem is the List
type hint. Without a type parameter, this is interpreted as List[Any]
, disabling the checks you wanted. (Any
is basically the "don't check me" type.)
If you want mypy to treat lst
as "list of arbitrarily mixed types, with type checking", you should use List[object]
.