pyright
pyright copied to clipboard
False positive of incompatible types (about generic protocol) since 1.1.337
Describe the bug
See sample code below.
In function f
, v1
can be assigned to v0
and v2
can be assigned to v1
, but v2
cannot be directly assigned to v0
.
Code or Screenshots
from typing import Generic, Protocol, TypeVar
V_co = TypeVar("V_co", covariant=True)
class C(Generic[V_co]):
def f(self, /) -> V_co: ...
class P(Protocol[V_co]):
def f(self, /) -> V_co: ...
def f(
v0: C[C[object]],
v1: C[P[object]],
v2: P[P[object]],
):
_ = v0, v1, v2
if 0:
v1 = v0
elif 0:
v2 = v1
elif 0:
v2 = v0 # False report.
VS Code extension or command-line
This issue appears since version 1.1.337. It doesn't appear with version from 1.1.292 through 1.1.336.
If the sample code is modified as below --
from typing import Generic, Protocol, TypeVar
V_co = TypeVar("V_co", covariant=True)
class C(Generic[V_co]):
def f(self, /) -> V_co: ...
class P0(Protocol[V_co]):
def f(self, /) -> V_co: ...
class P1(Protocol[V_co]):
def f(self, /) -> V_co: ...
def f(
v0: C[C[object]],
v1: C[P0[object]],
v2: P1[P0[object]],
):
_ = v0, v1, v2
if 0:
v1 = v0
elif 0:
v2 = v1
elif 0:
v2 = v0
No error is reported.
So I suspict there is some data pollution among generic types of a same protocol.
Thanks, I really appreciate the work you put into writing great bug reports.
I've narrowed the regression to this commit. The issue is related to the constraint solver, which is conflating the type variable of the "inner" P
with that of the "outer" P
in the annotation P[P[object]]
.
I'm glad to help.
This is addressed in pyright 1.1.364.