-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
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) # OKIt 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).