Open
Description
Mypy behaves unexpectedly when caller provides a list expression that has non-int items to a function that accepts Union[List[int], T]
:
from typing import *
T = TypeVar('T')
def f(a: Union[List[int], T]) -> T: pass
f([1]) # OK
f(['']) # E: List item 0 has incompatible type "str" <<--- unexpected
a = ['']
f(a) # OK
It would be better if mypy would always recognize that List[str]
is a valid argument for f
. However, if given a []
argument the inferred return type should be List[int]
.
This was originally discussed at #3501 (comment).