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

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

Closed
Azureblade3808 opened this issue Dec 18, 2023 · 4 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Azureblade3808
Copy link
Contributor

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 Azureblade3808 added the bug Something isn't working label Dec 18, 2023
@Azureblade3808
Copy link
Contributor Author

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.

@erictraut
Copy link
Collaborator

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

@Azureblade3808
Copy link
Contributor Author

I'm glad to help.

erictraut added a commit that referenced this issue May 21, 2024
…tions that involve nested protocols (such as `P[P[T]]`). This addresses #6767.
erictraut added a commit that referenced this issue May 21, 2024
…tions that involve nested protocols (such as `P[P[T]]`). This addresses #6767. (#7963)
@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label May 21, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.364.

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