Stewori / pytypes

Typing-toolbox for Python 3 _and_ 2.7 w.r.t. PEP 484.
Apache License 2.0
200 stars 20 forks source link

Infer return type of generic functions based on input #115

Open betodealmeida opened 2 years ago

betodealmeida commented 2 years ago

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?