pyright icon indicating copy to clipboard operation
pyright copied to clipboard

False positive of incompatible types (about generic protocol) since 1.1.337

Open Azureblade3808 opened this issue 1 year ago • 3 comments

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.

Pyright Playground

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.

Azureblade3808 avatar Dec 18 '23 06:12 Azureblade3808

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

Pyright Playground

No error is reported.

So I suspict there is some data pollution among generic types of a same protocol.

Azureblade3808 avatar Dec 18 '23 07:12 Azureblade3808

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]].

erictraut avatar Dec 18 '23 07:12 erictraut

I'm glad to help.

Azureblade3808 avatar Dec 18 '23 15:12 Azureblade3808

This is addressed in pyright 1.1.364.

erictraut avatar May 22 '24 02:05 erictraut