Dict key autocomplete not working when importing the dictionnary #4771
Answered
by
erictraut
ClementJ18
asked this question in
Q&A
-
Beta Was this translation helpful? Give feedback.
Answered by
erictraut
Aug 29, 2023
Replies: 1 comment 2 replies
-
Seems like a bug but there is a workaround. If you create the dict as a TypedDict then the completions will work elsewhere: from typing import TypedDict
class Enemies(TypedDict):
Goblin: int
Hobgoblin: int
the_dict: Enemies = {
"Goblin": 1,
"Hobgoblin": 2
} Usage: |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this is working as intended. The completion provider (the component that presents completion suggestions) bases its suggestions on type information. With an untyped dict such as
the_dict
in the first example, the inferred type is simplydict[str, int]
. That type doesn't contain any information about the keys in the dict. And since it's a mutable type, the keys could be changed dynamically.IIRC, there was some special-case logic added to pylance's completion provider for locally-assigned dict expressions that allow it to offer key name suggestions from the dict expression within the module. This explains why this works locally but doesn't work across module boundaries.
Defining a …