microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 766 forks source link

Support for `**kwargs` autocompletion when using Unpack[T] #2541

Closed dgellow closed 2 years ago

dgellow commented 2 years ago

Environment: Pylance version v2022.3.4, from vscode.

I'm not sure if it is a bug or would be considered a new feature. I understood from https://github.com/microsoft/pyright/issues/3002 that typed **kwargs are now supported by pylance/pyright. Thank you for this!

Now when I try the example mentioned at https://github.com/microsoft/pyright/issues/3002#issuecomment-1046100462 I see that while type checking seem to work correctly, the autocompletion doesn't seem to work as I would expect.

Let's say I have this code in vscode, with Pylance enabled:

from typing_extensions import Unpack, TypedDict, Literal

class MyKwargs(TypedDict):
    foo: Literal["hello", "nopnop"]
    bar: int

def baz(**kwargs: Unpack[MyKwargs]) -> None:
    pass

baz(foo="hello", bar=3)
baz(|) # 👈 My caret is here

I would expect that when I trigger the autocompletion via ctrl+space I would get suggested foo= and bar=, but instead the only relevant suggestion is kwargs=.

image

When I trigger the autocompletion for MyKwargs() arguments I get something correct, the issue seem to only be when using Unpack[T]: image

It would be nice to have the list of keys of the unpacked typed dict :)

rattrayalex commented 2 years ago

Related issue thread in mypy: https://github.com/python/mypy/issues/4441

erictraut commented 2 years ago

This feature (using a TypedDict to specify the value of **kwargs) is experimental and hasn't yet been ratified. It's supported only as a proof of concept to inform an eventual PEP. Until it becomes standard, I don't think it's likely to get much adoption, and it's probably not worth investing the time in adding completion suggestion support. But it may eventually make sense to do so if we get sufficient signal from users.

Since this is not a core type checking issue, I'm going to transfer this request to the pylance-release repo for consideration and tracking.

dgellow commented 2 years ago

Sounds good, thanks for the quick response. Would you accept a pull request, or do you consider the feature too experimental and would prefer to first wait for the feature to become standard? (or at least for a PEP to be accepted)

bschnurr commented 2 years ago

We would prefer to wait for it to become standard. If that happens, please reopen if you would still like it. Thank you.

nitedani commented 2 years ago

In my opinion, this is a must have feature for the future of python. Typescript had this for 10 years and it's an essential feature of the language. I'm happy to see python making progress in supporting better typing.

erictraut commented 2 years ago

The good news is that this proposal is one step closer to becoming standardized in Python. It is now a draft PEP, and if it's accepted, it will become part of Python 3.12.

zanieb commented 1 year ago

@bschnurr it looks like this PEP has now been accepted and is on track to be a standard in Python 3.12. See https://peps.python.org/pep-0692/

Would you be willing to open this issue again?

rchiodo commented 1 year ago

We actually implemented this. It should be working in our latest prerelease.

image

zanieb commented 1 year ago

@rchiodo Oh sweet! I had just tried this on VSCode but I'm not on any preview versions. Thanks for the quick response.