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

Pyright fails to bind contravariant generic type in generic function #7212

Closed
LeeeeT opened this issue Feb 6, 2024 · 2 comments
Closed

Pyright fails to bind contravariant generic type in generic function #7212

LeeeeT opened this issue Feb 6, 2024 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@LeeeeT
Copy link

LeeeeT commented Feb 6, 2024

Pyright is unable to infer the type of bound type variable in generic function under certain conditions.

from collections.abc import Callable
from typing import TypeVar, Generic

T = TypeVar('T', contravariant=True)

class C(Generic[T]):
    pass

def f[A](c: Callable[[A], None], v: A):
    pass

def g[A](c: C[A]):
    pass

def h(c: C[int]):
    f(g, c)
file.py:16:10 - error: Argument of type "C[int]" cannot be assigned to parameter "v" of type "A@f" in function "f"
  "C[int]" is incompatible with "C[A@g]"
    Type parameter "T@C" is contravariant, but "int" is not a supertype of "A@g"
      "object*" is incompatible with "int"

A@g should be bound to int, A@f should be bound to C[int] and no errors should be produced.

The bug is somehow related to the fact that T is contravariant. If I make it covariant or invariant, it does work fine.

@LeeeeT LeeeeT added the bug Something isn't working label Feb 6, 2024
@erictraut
Copy link
Collaborator

Thanks for the bug report. I'm able to repro the problem, and I agree it's a bug. I'll investigate further.

erictraut added a commit that referenced this issue May 31, 2024
…stances involving a call to a higher-order generic function with another function that involves a class with a contravariant type variable. This addresses #7212.
@erictraut erictraut added addressed in next version Issue is fixed and will appear in next published version and removed constraint solver labels May 31, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.366.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants