PyCharm has no idea what a dict contains if you populate it dynamically. Therefore, you must tell PyCharm in advance about the dict keys. Prodict does just that for PyCharm's hint, so you get code completion.
First, if you want to have access to the response object, you need to get the json response and convert it to a dict . This is achieved using the .json() method from requests as follows:
response = requests.get("https://some.restservice.com/user/1").json()
Ok, we loaded it into a dict object, now you can access the keys with the syntax in brackets:
print(response['name'])
Since you are requesting code completion automatically, you certainly need to give PyCharm a hint about dict keys. If you already know the response scheme, you can use Prodict to prompt PyCharm:
class Response(Prodict): name: str price: float response_dict = requests.get("https://some.restservice.com/user/1").json() response = Response.from_dict(response_dict) print(response.name) print(response.price)
In the above code, the name and price attributes are compiled automatically.
If you don't know the response scheme, you can still use dot notation to access dict attributes like this:
response_dict = requests.get("https://some.restservice.com/user/1").json() response = Prodict.from_dict(response_dict) print(response.name)
But code completion will not be available because PyCharm cannot know what this circuit is.
Moreover, the Prodict class is derived directly from dict , so you can use it as a dict as well.
This is a screenshot from the Prodict repository that illustrates code completion:

Disclaimer : I am the author of Prodict.
Ramazan polat
source share