consider the following example
from typing import Callable, Iterable, TypeVar, Any, overload, Tuple, TYPE_CHECKING
from itertools import islice
T = TypeVar("T")
@overload
def mytake(n:int, iterable:Iterable[T]) -> Tuple[T,...]:...
@overload
def mytake(n:int, iterable:Iterable[T], container:Callable[[Iterable[T]],Any]) -> Any:...
def mytake(n:int, iterable:Iterable[T], container:Callable[[Iterable[T]],Any]=tuple) -> Any:
return container(islice(iterable,n))
if TYPE_CHECKING:
pass
else:
reveal_type = print
#a:tuple[int,...]
a = mytake(10,range(100))
reveal_type(a)
#b:list[int]
b = mytake(10,range(100), list)
reveal_type(b)#desired to be list[int]
#c:int
c = mytake(10,range(100),sum)
reveal_type(c)#desired to be int
help(mytake)
that ouput
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
45
Help on function mytake in module __main__:
mytake(n: int, iterable: Iterable[~T], container: Callable[[Iterable[~T]], Any] = <class 'tuple'>) -> Any
Basically just as desire, but I would like that mypy was the same
C:\Users\copperfield\Desktop\mypy_tests>mypy test6.py
test6.py:21: note: Revealed type is "builtins.tuple[builtins.int, ...]"
test6.py:25: note: Revealed type is "Any"
test6.py:29: note: Revealed type is "Any"
Success: no issues found in 1 source file
C:\Users\copperfield\Desktop\mypy_tests>
Beside type hinting the variables themselves, is there a way to make it so mypy correctly detect the types here?