1

Is anybody able to get suggestions after typing x. using code below? E.g. name

import json
from types import SimpleNamespace


data = '{"name": "John Smith", "hometown": {"name": "New York", "id": 123}}'

# Parse JSON into an object with attributes corresponding to dict keys.
x = json.loads(data, object_hook=lambda d: SimpleNamespace(**d))
print(x.name, x.hometown.name, x.hometown.id)

The upper example stems from here

Another promising approach is presented here, yet no auto-completion as well

I'm using lsp-pyright

nega
  • 3,091
  • 15
  • 21
jjk
  • 705
  • 4
  • 16

1 Answers1

0

Auto-completion, in this case, is not something you're going to get from static parsers. As far as they're concerned data is just a string, and json.loads() returns an "object". The "making the string of JSON data named 'data' into a python object and assigning it to 'x'" part requires executing json.loads() which static parsers don't do because that would be "dynamic", à la what happens when input your sample code into the python/ipython REPL. After hitting Return on your x = json.loads(...) the python REPL runs json.loads() on the string data, creates the python object, and assigns it to x. This is why at the next and future statements you can autocomplete on x.<tab> there. If your example data statement was a python dict, and your x = json.loads(...) was maybe a x = map(..), then yes you would get autocompletion on your print() line.

tl;dr

What you're asking for is outside the job of static parsers, which are typically the backend of your "lsp server", therefore they don't understand the input and can't accommodate your expectations.

nega
  • 3,091
  • 15
  • 21
  • maybe somewhere out there is an `ipython` lsp backend that would suit your needs in this case. though i think there might be a huge speed/convenience trade off. – nega Feb 09 '23 at 04:23