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

Pylance behaviour on overloads doesn't match pyrights behaviour #6277

Closed
yowoda opened this issue Aug 19, 2024 · 5 comments
Closed

Pylance behaviour on overloads doesn't match pyrights behaviour #6277

yowoda opened this issue Aug 19, 2024 · 5 comments
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@yowoda
Copy link

yowoda commented Aug 19, 2024

Environment data

  • Language Server version: 2024.8.1
  • OS and version: win32 x64
  • Python version (and distribution if applicable, e.g. Anaconda):
  • python.analysis.indexing: true
  • python.analysis.typeCheckingMode: off

Code Snippet

import typing as t
from typing_extensions import ParamSpec, Concatenate

_OriginArgsP = ParamSpec("_OriginArgsP")
_OriginSelfT = t.TypeVar("_OriginSelfT")
_OriginReturnT = t.TypeVar("_OriginReturnT")

_TargetSelfT = t.TypeVar("_TargetSelfT")
_TargetArgsP = ParamSpec("_TargetArgsP")
_NewReturnT = t.TypeVar("_NewReturnT")

@t.overload
def copy_method_signature(
    origin: t.Callable[Concatenate[_OriginSelfT, _OriginArgsP], _OriginReturnT], *, return_type: None=..., skip_self: t.Literal[False]=...
) -> t.Callable[[t.Callable[Concatenate[_TargetSelfT, _TargetArgsP], t.Any]], t.Callable[Concatenate[_TargetSelfT, _OriginArgsP], _OriginReturnT]]:
    """A method copies the full signature from another method"""

@t.overload
def copy_method_signature(
    origin: t.Callable[Concatenate[_OriginSelfT, _OriginArgsP], _OriginReturnT], *, return_type: type[_NewReturnT], skip_self: t.Literal[False]=...
) -> t.Callable[[t.Callable[Concatenate[_TargetSelfT, _TargetArgsP], t.Any]], t.Callable[Concatenate[_TargetSelfT, _OriginArgsP], _NewReturnT]]:
    """A method copies the parameter signature from another method but overrides the return type"""

@t.overload
def copy_method_signature(
    origin: t.Callable[Concatenate[_OriginSelfT, _OriginArgsP], _OriginReturnT], *, return_type: None=..., skip_self: t.Literal[True]
) -> t.Callable[[t.Callable[_TargetArgsP, t.Any]], t.Callable[_OriginArgsP, _OriginReturnT]]:
    """A function copies the full signature from another method"""

@t.overload
def copy_method_signature(
    origin: t.Callable[Concatenate[_OriginSelfT, _OriginArgsP], _OriginReturnT], *, return_type: type[_NewReturnT], skip_self: t.Literal[True]
) -> t.Callable[[t.Callable[_TargetArgsP, t.Any]], t.Callable[_OriginArgsP, _NewReturnT]]:
    """A function copies the paramater signature from another method but overrides the return type"""

@t.no_type_check
def copy_method_signature(
    origin, *, return_type=None, skip_self=False
):
    @t.no_type_check
    def inner(target):
        return target
    
    return inner

class A:
    @t.overload
    def method(self, a: int)  -> None:
        ...

    @t.overload
    def method(self, a: str) -> int:
        ...

    def method(self, a: t.Union[int, str]) -> t.Optional[int]:
        ...

@copy_method_signature(A.method, return_type=int, skip_self=True)
@t.no_type_check
def hello(*args, **kwargs):
    ...


reveal_type(hello)

Repro Steps

  1. Hover over the copy_method_signature decorator

Expected behavior

No error

Actual behavior

No overloads for "copy_method_signature" match the provided argumentsPylance[reportCallIssue](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportCallIssue) method.py(31, 5): Overload 4 is the closest match

  1. Hover over the reveal_type call

Expected behaviour

Overload[(a: int) -> int, (a: str) -> int] as pointed out by pyright in the command line:
method.py:64:13 - information: Type of "hello" is "Overload[(a: int) -> int, (a: str) -> int]" 0 errors, 0 warnings, 1 information

Actual behaviour

Type of "hello" is "(...) -> None"

Logs

XXX
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Aug 19, 2024
@yowoda
Copy link
Author

yowoda commented Aug 19, 2024

This only seems to happen if you pass in arguments to the copy_method_signature call

@copy_method_signature(A.method)
@t.no_type_check
def hello(*args, **kwargs):
    ...

pylance seems to have no problem with this

@bschnurr
Copy link
Member

thanks for the report. pylance didn't get update last week and I think pyright recently updated its overload logic, so I'm guessing that is the reason for the disagreement. In the mean time you can check which version of pyright pylance is using in the python language server output window.

@yowoda
Copy link
Author

yowoda commented Aug 19, 2024

Yup, pylance is using version 1.1.373, seems like this bug was fixed in pyright version 1.1.374

@debonte
Copy link
Contributor

debonte commented Aug 19, 2024

Yup, pylance is using version 1.1.373, seems like this bug was fixed in pyright version 1.1.374

Pylance's latest prerelease build (2024.8.101) is using Pyright 1.1.375.

@yowoda
Copy link
Author

yowoda commented Aug 19, 2024

Ah yep, can confirm it's working on the prerelease build, thanks a lot!

@yowoda yowoda closed this as completed Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

3 participants