pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Call signature narrowing for overloaded methods/functions

Open BenjaminMidtvedt opened this issue 10 months ago • 1 comments

Pylance reports all available overloads for overloaded methods. However, as arguments are provided, some of them can be discarded. Example:

@overload
def f(x: Literal["foo"], foo: int = 2) -> None: ...

@overload
def f(x: Literal["bar"], bar: int = 3) -> None: ...

def f(x: str, *args: int, **kwargs: int) -> None: ...

My expectation is that when entering the first literal string:

f("bar", 

that pylance would suggest the call signature of the second overload directly. However, currently, the impossible first overload is still displayed:

image

Use case

It's a pretty common python pattern to dispatch to different implementations based on the value of some "mode" or "task" parameter. It is also commonly used in __new__ to create one of several classes. This would greatly improve the user experience working with these patterns.

BenjaminMidtvedt avatar Apr 07 '24 18:04 BenjaminMidtvedt

This is the same as https://github.com/microsoft/pylance-release/discussions/4148, and similar to https://github.com/microsoft/pylance-release/issues/4428

Viicos avatar Apr 09 '24 11:04 Viicos

Thanks for reporting the issue, this is indeed a dupe of https://github.com/microsoft/pylance-release/discussions/4148. Please upvote the idea if you'd like to see it being supported, thanks!

StellaHuang95 avatar Jul 16 '24 19:07 StellaHuang95