Open
Description
I believe the below code should be allowed:
from typing import *
T_co = TypeVar('T_co', covariant=True)
class Col(Generic[T_co]):
def __init__(self, items: Iterable[T_co]) -> None:
self.items = items
def any(self, pred: Callable[[T_co], bool]) -> bool:
return any(map(pred, self.items))
def any_zero(self, fn: Callable[[T_co], int]) -> bool:
def pred(x: T_co) -> bool: # error: Cannot use type variable as a parameter
return fn(x) == 0
return self.any(pred)
I believe the purpose of this error is to disallow breaking variance, e.g.:
def unsafe(self) -> Callable[[T_co], int]:
def fn(t: T_co) -> int:
return id(t)
return fn
However, restricting variance on nested functions has both false positives (first example), and false negatives:
class Unsafe(Generic[T_co]):
def __init__(self, fn: Callable[[T_co], int]) -> None:
self.fn = fn
def get_fn(self) -> Callable[[T_co], int]:
return self.fn
The above code has no error, but is not type safe. I believe the proper way to check for this is to look at the signatures of each exposed method and verify that the type parameter is not used with the wrong parity.
mypy 0.740, python 3.7.5