pytypes icon indicating copy to clipboard operation
pytypes copied to clipboard

Infer return type of generic functions based on input

Open betodealmeida opened this issue 3 years ago • 0 comments

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?

betodealmeida avatar Jan 04 '22 02:01 betodealmeida