facebook / facebook/pyrefly

`variance-mismatch` false positive on constrained typevar in protocol

Open
#3,655 0 comments 0 reactions 0 assignees View on GitHub
typechecking
Dominant language
Rust
Stars
7k
Forks
516
PR merge metrics
No merged PRs in 30d

Description

### Describe the Bug

```python
from typing import Protocol, TypeVar

T = TypeVar("T", str, bytes)

class Foo(Protocol[T]): # error: Type variable `T` in class `Foo` is declared as invariant, but could be covariant based on its usage
def value(self) -> T: ...
```

variance is not a thing on constrained typevars because they can only be specialized to one of the exact types (not a subtype or supertype)

(pyright has the same issue: https://github.com/DetachHead/basedpyright/issues/898)

### Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0AFag1wBjXFAA0dACrMYANVSUAOqvRqV6GXQC8s%2BUsoAKFSBlnpcBpWnZSDeAEpNG9aKio4cOgDFcuMZCuCLiUADaMgC6Toh0AMR0MJTUlHFyxDB0AG7KEKjYsHQABjLFnOh0Hl4%2Bxf645RA%2BmDDVlDCYdF4VuZT56Ax2AK784kNQndhZ4r39-NheHXS4lRAMPkNobDCudHt0LWA5qFBDMMZwMFBgTnQAtAB8snGEryCSIGTtYFCkhCI0KAURICUjfX50NBYPD4KorSBsIaUVAMCArQhaRIAZRgWQAFgwGMQ4IgAPSkr4wH5-XhsUkwdCkzBiOCk8ToBFIlFoxl0MC8Lq5aAFIrsznI1ErZbESXoOAY9BkBh4lZ3bLJOA8vR0MwAZkIAEYAExmLThZKpOBRLRDdBcHh8Dp3TAQdqiVHq7UAcggbHQvBgXq0-oYd3aAEchq6nQBrGCkO6oUSieA%2BfRegDuynQQbt3CgEFEa0T6FI3otvFzMHwxALRdDGDL6YrlC9IAAvh8kx6YL5oDAKFCcAQSOR20A

### (Only applicable for extension issues) IDE Information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.