Bug
from typing import TypeVar, Union
T = TypeVar("T")
def get_or_str(value: T) -> Union[str, T]:
return value
def f(b: bool) -> Union[str, bool]:
return "" if b else get_or_str(True)
a.py:12: error: Argument 1 to "get_or_str" has incompatible type "bool"; expected "str"
No error.
0.710+dev.5f08ccf029aa3046b15e1afc60743ba692e4758d (from master)
None
There is a similar error when trying to return a Sequence using tuples of different lengths:
def f(b: bool) -> Sequence[int]:
return (1,) if b else (2, 3)
a.py:4: error: Incompatible return value type (got "object", expected "Sequence[int]")
The tuple example is a separate (relatively simple, but old) issue. The original example is just another case (among dozens) where single-bin inference would help.
I think we should really do this.
A similar problem:
from typing import Union
def f(a: Union[str, int], b: Union[str, int]) -> Union[str, float]:
return a if isinstance(a, str) else b if isinstance(b, str) else a / b
a.py:4: error: Incompatible return value type (got "object", expected "Union[str, float]")
Likely the same issue:
from typing import Callable, Optional, TypeVar
T = TypeVar("T")
def f(
a: Callable[[], T],
b: Callable[[], T]
) -> T:
return a()
op: Optional[int] = f(lambda: 1, lambda: None)
a.py:11: error: Incompatible return value type (got "None", expected "int")
Works bare T instead of callables.