Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Covariant type variable error in nested function #8191

Open
branpk opened this issue Dec 21, 2019 · 1 comment
Open

Covariant type variable error in nested function #8191

branpk opened this issue Dec 21, 2019 · 1 comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables

Comments

@branpk
Copy link

branpk commented Dec 21, 2019

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

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 6, 2020

Yeah, variance checks are incomplete and your example seems valid to me. This issue is related to #734, but the nested function case seems like a separate issue so I'm keeping this open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-type-variables
Projects
None yet
Development

No branches or pull requests

2 participants