pytypes
pytypes copied to clipboard
Infer return type of generic functions based on input
Is it possible to infer the return type based on the input, for generic functions?
Eg, with this classic example:
from typing import TypeVar, Sequence
T = TypeVar('T') # Declare type variable
def first(seq: Sequence[T]) -> T: # Generic function
return seq[0]
If I know that seq has type Sequence[int], how can I infer that first would return int in that case?