Open Why-not-now opened 1 year ago
Could a pylance team member please transfer to the pyright project? This is a core type checker issue.
Here's a simplified repro:
from typing import TypeVar, Protocol
T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)
class FalsePositive(Protocol[T]):
var: T | None
class Positive(Protocol[T_co]):
var: T_co | None
And here's an even simpler repro that demonstrates the underlying problem:
from typing import TypeVar
T = TypeVar("T")
def func(x: list[T | None]) -> T | None:
y: list[object | None] = x # This should generate an error but does not
return x[0]
Thanks for the bug report. This will be fixed in the next release of pyright and a future release of pylance.
Thank you, do I close it now?
Please leave it open. The pylance team will close it once it's in a released version of pylance.
Environment data
Code Snippet
Expected behavior
Only the class
Positive
to give a warning thatType variable "T_co" used in generic protocol "Positive" should be invariant
, just like mypy.Actual behavior
Opposite is true, class
Positive
does not give a warning. ClassFalsePositive
gives a warning thatType variable "T" used in generic protocol "FalsePositive" should be covariant
.Logs