Pycharm: code completion does not give recommendations

Let's say that I am working with the python "queries" library.

req = requests.get("http://google.com") 

Now after that, if I type req. I should get a list of all the methods that I can access. But for some reason, I do not do this, even if I manually press ctrl-space.

If I try this in ipython, I will get autocomplete recommendations. Even if I try it through the python built-in console in pycharm, I get recommendations.

Why is this happening?

+12
source share
4 answers

Since Python is a dynamically typed language, you need to make sure that it can determine the type of objects and correctly check the libraries on your system. Try to make it clear what type of object is in your code.

Starting with version PyCharm 2.7 (when versions were numbers), you can enable type detection at runtime - PyCharm connects to your program at runtime (during debugging) and checks the types of variables as they are used.

You can enable this by going to the settings, going to the Build, Run, Deploy section, and then to the Python Debugger subsection and turning on Collecting Runtime Type Information to Understand Code.

The settings screen of PyCharm open to show the relevant setting.

Obviously, it’s worth noting that this is not ideal - if you make changes, they will not be updated until the code is executed, and it can tell you only about the values ​​that it saw - other code paths that you have not tried can install other types.

You can also tell PyCharm by using lines of Epydoc or Sphinx-style documents that contain information about the parameters and return types. PyCharm will use them to improve checks.

Python has also received support for function annotations since Python 3. They can be used for type hints according to PEP 484 . See the typing module for more information. This is more formal, so it can also be used for tools like mypy , which is a type checking tool that can programmatically check these types for consistency, providing Python with optional TypeScript-style static typing.

+21
source

Python is a dynamically typed language, which means that the get function does not declare its return type. When you enter the code in IPython or in the PyCharm console, the code is actually executed, and you can check the instance of the object in the running interpreter and get a list of its methods. When you enter code into the PyCharm or any other Python IDE, it fails, and only static analysis can be used to infer the return type of the method. This is not possible in all cases.

+7
source

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:

Prodict code completion

Disclaimer : I am the author of Prodict.

0
source

if just detects methods or variables and ... while writing some of this: File-> Settings β†’ Editor β†’ General β†’ Code completion at the top of an open window, uncheck

0
source

All Articles