thorwhalen / umpyre

Materials for python coaching
MIT License
1 stars 0 forks source link

A failed attempt at using `typing.TypeVar` #56

Open thorwhalen opened 3 years ago

thorwhalen commented 3 years ago
from typing import TypeVar, Iterable

T = TypeVar('T')
ClosedAggregator = Callable[[Iterable[T]], T]

def representative(s: Iterable[T], aggr: ClosedAggregator) -> Iterable[T]:
    a = aggr(s)
    return (x / a for x in s)

def first(s):
    return s[0]

representative([1,2,3], first)

def not_an_aggr(s):
    return s[:4]

representative([1,2,3], not_an_aggr)  # !!! No linting happens! ???

But will happen if I explicitly tell it that the function doesn't return T


def not_an_aggr(s) -> Iterable[T]:
    return s[:4]

representative([1,2,3], not_an_aggr)  # linting happens

Interestingly enough, it doesn't complain with -> int, but does complain with list (but whose to say that T is not a list?)