Closed
Description
I'm playing with mypy, trying to do some things, and I've getted some strange behaviour (I think).
I'm trying to do a function that takes a list and then apply map function. The trick is that the list can be of two different types.
The first try was something like that:
def f(L: Union[List[int], List[str]]) -> None:
l = map(t, L)
@overload
def t(L: int) -> List[int]:
return [L]
@overload
def t(L: str) -> List[str]:
return [L]
The fail that getted was this one:
test.py, line 8: Argument 2 to "map" has incompatible type "Union[List[int], List[str]]"; expected Iterable[None]
Then, I tried to do it with a typevar:
T = typevar('T', values=(int, str))
def f(L: List[T]) -> None:
l = map(t, L)
@overload
def t(L: int) -> List[int]:
return [L]
@overload
def t(L: str) -> List[str]:
return [L]
And now I've getted that error:
test.py, line 8: Argument 1 to "map" has incompatible type functionlike; expected Function[["str"] -> List[int]]
If I add the function that 'mypy' says:
@overload
def t(L: str) -> List[int]:
return [1]
And now the typecheck pass.
If I write the overloaded functions backwards, mypy expects other thing:
@overload
def t(L: str) -> List[str]:
return [L]
@overload
def t(L: int) -> List[int]:
return [L]
Expected:
test.py, line 8: Argument 1 to "map" has incompatible type functionlike; expected Function[["int"] -> List[str]]
I don't really know why that's going on, but I think that is a strange behaviour :)