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

use overload information from class definition to resolve type parameters in cases of Protocols #989

Open
KotlinIsland opened this issue Jan 9, 2025 · 0 comments
Labels
rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules

Comments

@KotlinIsland
Copy link
Collaborator

KotlinIsland commented Jan 9, 2025

from typing import Protocol, overload

class A:
    @overload
    def f(self, x: int) -> int: ...
    @overload
    def f(self, x: str) -> str: ...

    def f(self, x: int | str) -> int | str: ...

class CanF[X, R](Protocol):
    def f(self, x: X) -> R: ...

def f[X, R](s: CanF[X, R], x: X) -> R:
    return s.f(x)

a = A()
reveal_type(a.f(1))  # int
reveal_type(a.f(""))  # str
reveal_type(f(a, 1))  # int
reveal_type(f(a, ""))  # int - Argument of type "Literal['']" cannot be assigned to parameter "x" of type "X@f" in function "f"

here, f(a, "") should resolve to Callable[[A, str], str] / X = str, R= str because it can retrieve information from the definition of A to find a valid solution

a more complicated scenario involving intersections would look like:

from typing import Never, Protocol, overload

class A:
    @overload
    def f(self, x: int) -> int: ...

    @overload
    def f(self, x: str) -> str: ...

    def f(self, x: int | str) -> int | str: ...

class CanF[R](Protocol):
    def f(self, x: Never) -> R: ...

def f[X, R](s: CanF[R]) -> CanF[R]: ...

a = A()
reveal_type(a.f)  # Overload[(x: int) -> int, (x: str) -> str]
reveal_type(f(a))  # CanF[int], ideally CanF[int] & CanF[str]

credit to @jorenham for the discovery of this defect

@KotlinIsland KotlinIsland added rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules labels Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rejected upstream also a bug in pyright/pylance or feature that isn't in pyright/pylance - they refused to address it type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules
Projects
None yet
Development

No branches or pull requests

1 participant