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

Provide key completions for TypedDict dictionaries #827

Closed
brettcannon opened this issue Jan 12, 2021 · 14 comments
Closed

Provide key completions for TypedDict dictionaries #827

brettcannon opened this issue Jan 12, 2021 · 14 comments
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@brettcannon
Copy link
Member

E.g., for the following code:

import typing


class Spam(typing.TypedDict):
    attr: str
    other_attr: int


def func(arg: Spam) -> None:
    print(arg[])

I would expect to be able to get a completion from within arg[] as the possible set of keys are known.

@brettcannon brettcannon changed the title Provide key completions for TypedDict objects Provide key completions for TypedDict dictionaries Jan 12, 2021
@jakebailey
Copy link
Member

I think this may be a dupe of #654.

@jakebailey
Copy link
Member

Not entirely, as that's for building the dict; this is for getting values from it. I'm surprised we don't do this, but I guess this is a string literal completion in [] which isn't plain . member access.

@brettcannon
Copy link
Member Author

Yep, it's completing to a string in an index context and subtly different from #654 for the reasons you listed.

@jakebailey jakebailey added the enhancement New feature or request label Jan 12, 2021
@github-actions github-actions bot removed the triage label Jan 12, 2021
@erictraut
Copy link
Contributor

This feature is already implemented, but the usability is not great currently. The problem is that the suggestion is provided only when you type the first quote character, but the client doesn't automatically trigger a suggestion request at that point. If you place the cursor between the [ and the ] and type a quote, then press control-space to force the completion suggestions to be displayed, you'll actually see the available TypedDict attributes appear in the completion list.

Any suggestions for how to improve the usability here?

@jakebailey
Copy link
Member

We do declare that [ is a character that can trigger completion: https://github.com/microsoft/pyright/blob/86221745ca1e9122e4953ed2c63515b4de374bbf/packages/pyright-internal/src/languageServerBase.ts#L960

I think what would be an improvement would be at that point to quite literally offer up the string literals as full completions, rather requiring that a user types a quote. For example, TS does something like this:

image

@jakebailey
Copy link
Member

(The above is an example of making the object, so that's really #654, but I just mean to point at the completion itself.)

@erictraut
Copy link
Contributor

Yeah, that's a good suggestion. I just checked in a change that implements this.

@jakebailey
Copy link
Member

A gotcha is which quote character to choose; I think TS probably scans the file to figure out the predominant one to offer.

I don't see the commit yet on pyright, but I'll take a look once it's there.

@erictraut
Copy link
Contributor

Pyright also chooses the predominant quote in the document.

@jakebailey
Copy link
Member

jakebailey commented Jan 12, 2021

Appears to work as expected:

image

Not sure how I feel about ' being the default choice when there aren't any quotes, though. A nit.

image

@jakebailey jakebailey added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label Jan 12, 2021
@jakebailey
Copy link
Member

jakebailey commented Jan 12, 2021

Slightly curious which completion item kind is being used; TS has that horizontal line icon while we have the abc (which in my mind means "VS Code broke and is offering me word completions"). I think abc is CompletionItemKind.Text while the other is Enum (maybe). Can leave it to another time to compare the icons and fix that up, since we use quoted completions for other reasons as well.

EDIT: It's CompletionItemKind.Constant.

@brettcannon
Copy link
Member Author

Yeah, the use of abc as the completion type also threw me initially and made me think it was just picking up on terms in the file.

As for which quotes to use by default, best I can tell you is Black defaults to "/double quotes.

@jakebailey
Copy link
Member

My preference would be to swap this to Constant as Text is VSC's default "IDK" completion icon and that might be off-putting for some people. I'll see if I can change that for today (depending on if the power outage gets me back my machine...).

Reading the quote character, the parser seems to count and then have a boolean of predominantSingleQuoteCharacter which defaults to true if they're equal. That might be a further improvement later to change if we believe we want to be as close to black as possible in quote choicing.

@jakebailey
Copy link
Member

This issue has been fixed in version 2021.1.1, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202111-13-january-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants