-
-
Notifications
You must be signed in to change notification settings - Fork 512
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
Feature request: support TypedDict completions #1478
Comments
I think while the second one would be nice, that's quite a bit of additional code just for completions, so not saying to not do that, it's just probably less realistic. However the first one looks like we should definitely support it! If you look into it, one good example of this is probably the Django migration: https://github.com/davidhalter/jedi/pull/1467/files. Or the Enum stuff, see Lines 785 to 816 in 47e2cf9
In Python 3.8, the TypedDict is a
in
to Also your class will probably look something like this:
Please feel free to ask questions :) The primitives in Jedi are "filters" for finding stuff, "contexts" for being in somewhere (e.g. a FunctionContext), "values" (e.g. a Function) and "names" (e.g. a function name). Your entry point is |
I just realized that this is not really how it works. What I described works for attributes, but not for dict keys/values. That's a bit unfortunate. I have to think a bit about this |
Good to know, thanks for the response! I'll remain waiting as you ponder. |
All right. This took me quite a bit longer than I anticipated. Took me about 5-7 hours to figure out how to do this. Now it feels a bit hacky, but I guess it's still the right way. TypedDict itself is a big hack that breaks pretty much all rules of traditional meta classes. I have pushed a typeddict branch that you can checkout and work on. There's already a few tests. Some of them are passing. Your main task is to get those working. Everything after that is probably pretty easy. Work has to be done mostly in Also you will probably need to add a few tests to cover inheritance/multiple inheritance, but that can wait a bit. |
@davidhalter awesome, I'm excited to start working on this! Will try dedicate some significant time to it throughout this week. |
Ok, I'm a bit lost here. I've played around with Also, in case this helps at all, outside of Jedi, the only way I've been able to see the "attributes" defined within a typed dict is with from typing import TypedDict, get_type_hints
class Hello(TypedDict):
world: str
number: int
print(get_type_hints(Hello)) It results in: $ python script.py
{'world': <class 'str'>, 'number': <class 'int'>} I haven't found another way to find these "attributes", if you can call them that. |
Jedi doesn't execute anything, it parses using parso, so there's no reason to use You can get the annotation dict in Jedi in certain ways. But usually filters are better to actually get what you want. However, you're certainly right that you don't anything meaningful out of the filters I proposed to you. I mentioned So the way to get what you want at that point is to use Sorry, I really forgot that annotations in classes are actually instance attributes. |
Ok, I've got basic completion to work with your suggestions: https://github.com/pappasam/jedi/blob/typeddict/jedi/inference/gradual/typing.py#L395 An initial problem: given the mplementation, unlike regular dictionaries, Jedi doesn't know the type of the dictionary values coming from TypedDict: Any advice for how to preserve the typed information of each value? It's probably something simple I'm missing. |
For |
@davidhalter sorry, I tried to follow your above suggestion, but am completely lost about where |
@pappasam No problem :) I was referring to what I wrote above:
Maybe you find edge cases with a few tests. If you add tests for the getitem cases, please add them in |
Unfortunately, despite using the above example and trying some other shots in the dark, I'm still unable to get Jedi to return the type of the dictionary values coming from TypedDict. The previous example now returns nothing on completion. Either this is a bit more complicated than we thought, or I'm missing something obvious. Note: I changed |
I'll look into it. Thanks for trying! :) It's not easy to debug this kind of stuff. |
All right. I merged master into the typeddict branch and added a commit. I had to refactor the class filters for a bit, because they didn't properly work with what we wanted to do. It's pretty much the most complicated part of how finding names in Jedi works, so don't worry if you don't exactly understand what happened :) |
Merged #1495 |
Hi @davidhalter Here is my code example:
|
This is not done yet and part of #1740 |
Jedi now supports dictionary completions: #951
Unfortunately, for those of us trying to get the most out of
TypedDict
, the latest master does not support completions for TypedDicts. If we can get the dictionary keys of a TypedDict to autocomplete, I believe users would start using TypedDict a lot more.As a stretch goal, we could also consider completing dictionary keys as the dictionary is being created.
Happy to start chipping away at this if you can point me in a profitable direction. And great to see that we're getting dictionary key completion!!!
The text was updated successfully, but these errors were encountered: