-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
bugmypy got something wrongmypy got something wrongfalse-positivemypy gave an error on correct codemypy gave an error on correct codepriority-1-normaltopic-type-variables
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 fnHowever, 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.fnThe 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
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongfalse-positivemypy gave an error on correct codemypy gave an error on correct codepriority-1-normaltopic-type-variables