Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Show key type info when hovering over an index lookup on TypedDicts #4694

Closed
R-O-C-K-E-T opened this issue Aug 6, 2023 · 2 comments
Closed
Assignees

Comments

@R-O-C-K-E-T
Copy link

Current Behaviour

When hovering over an index operation into a TypedDict variable no type info for the key appears and definition is unavailable.

In contrast constructing a TypedDict object hovering over keys there does produce type info and definition.

from typing import TypedDict

class Test(TypedDict):
    field: str

test: Test = {
    'field': 'value',  # Hovering over 'field' gives type info and the definition is available
}
test['field']  # Hovering over 'field' in this case gives no info and no definition is found

Expected Behaviour

Hovering over a TypedDict key within an indexing operation should show the key type info and the definition should be available.
Screenshot from 2023-08-06 14-12-01

Motivating Use Case

Plain dictionaries often appear in legacy code bases and when migrating types into these code bases it is easier and less error prone to write TypedDicts to describe these dictionaries instead of migrating them into dataclasses or similar (IMO). Which do show type info when hovering over the property names.

Migrating this way works well to verify that all accesses and definitions are valid. However when writing code that indexes into one of these dictionaries, finding either the type, docstring or definition of a key is cumbersome. Since the developer must find the type of the indexed object the and manually scan through to the relevant key.

Future Ideas

Hover/Definition search for TypedDict.get

.get is often used when keys are marked NotRequired to skip verbose 'key' in obj checks. It may improve usability if key definitions were easily available.

from typing import TypedDict, NotRequired

class Test(TypedDict):
    field: NotRequired[str]

test: Test = {}
test.get('field')  # Hovering over 'field' could show type info for the 'field' key

Hover/Definition search for NamedTuple indexing

The same functionality for TypedDict could be applied to NamedTuple. The use case is weaker however, since the field can be directly referenced by name instead of by index.

from typing import NamedTuple

class Test(NamedTuple):
	first: int
	second: str

test = Test(1, 'second')
res = test[1]  # Hovering over 1 could show type info for the 'second' key
R-O-C-K-E-T referenced this issue in R-O-C-K-E-T/pyright Aug 6, 2023
Pulled out a helper for finding the type that the string is a key for. Copied
some logic for verifying that the StringNode is the only node within a
StringListNode from the completion logic. There appears to be some jank in the
find definition logic that doesn't verify if a StringNode is the only element
in a StringListNode. https://github.com/microsoft/pyright/issues/5655

https://github.com/microsoft/pyright/issues/5654
@erictraut
Copy link
Contributor

erictraut commented Aug 6, 2023

The hover provider shows the types of identifiers in your code. It doesn't do anything for a string literal because the type of that string literal is, well, a str. I suppose it could treat string literals specially if they are used to index a TypedDict, but that would be a special case and somewhat inconsistent.

In any case, I will transfer this request to the pylance-release project since it relates to a language server feature. The pylance team can decide whether to pursue this enhancement request or wait for additional feedback from pylance users.

@rchiodo or @debonte, could you transfer this to pylance-release? Thanks!

@rchiodo rchiodo transferred this issue from microsoft/pyright Aug 7, 2023
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Aug 7, 2023
@debonte debonte removed the needs repro Issue has not been reproduced yet label Aug 14, 2023
@debonte
Copy link
Contributor

debonte commented Aug 14, 2023

Moving this issue to discussion as an enhancement request for comments and upvotes.

@microsoft microsoft locked and limited conversation to collaborators Aug 14, 2023
@debonte debonte converted this issue into discussion #4720 Aug 14, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
3 participants