Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm: Code completion not giving recommendations

Say I'm working with the 'requests' Python library.

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

Now after this, if I type req., I'm supposed to get a list of all methods I can access. But for some reason I don't, even if I manually press Ctrl+Space.

If I try this in iPython, I get autocomplete recommendations. Even if I try it via the built in Python console in PyCharm, I get recommendations.

Why's this happening?

like image 360
user1265125 Avatar asked Feb 22 '13 11:02

user1265125


People also ask

Why is Pycharm not showing suggestions?

You can enable this by going to settings, going to the "Build, Execution, Deployment" section and then the "Python Debugger" subsection and enabling "Collect run-time types information for code insight".

How do I turn on autocomplete in Python?

(In Python Shell window, you can use TAB key besides the key combination of 'CTRL' and 'space' to invoke the built-in auto-completion feature.) Alternatively, you can choose the "Show Completions" in the main Edit menu to achieve the same as well.

How do I turn off suggestions in Pycharm?

In the Settings/Preferences dialog ( Ctrl+Alt+S ), select to Editor | Inspections. You can also press Ctrl+Alt+Shift+H and select Configure Inspections. Locate the disabled inspection in the list and select the checkbox next to it.

How do we activate the code completion feature?

If automatic completion is disabled, press Ctrl+Space or choose Code | Code Completion | Basic from the main menu. If necessary, press Ctrl+Space for the second time (or press Ctrl+Alt+Space ). This shows inaccessible classes and members as well as static fields and methods.


3 Answers

As Python is a dynamically typed language, you need to ensure it can work out what type things are, and inspect on the libraries on your system correctly. Try to make sure it's obvious what type the object is in your code.

One good way as of PyCharm 2.7 (back when versions were numbers) is to enable runtime type detection - PyCharm hooks into your program while it runs (while debugging), and checks the types of variables as they are used.

You can enable this by going to settings, going to the "Build, Execution, Deployment" section and then the "Python Debugger" subsection and enabling "Collect run-time types information for code insight".

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

Obviously it is worth noting that this isn't perfect - if you make changes, this won't be updated til the code is executed, and it can only tell you about values it has seen - other code paths you haven't tried could set other types.

You can also 'tell' PyCharm by using Epydoc or Sphinx style docstrings that contain information about parameter and return value types. PyCharm will use these to improve it's inspections.

Python also gained support for function annotations as of Python 3. These can be used for type hints as per PEP 484. See the typing module for more. This is more formal, so it can also be used for tools like mypy which a type checker that can programmatically check these types for consistency, giving Python a TypeScript-style optional static typing.

like image 158
Gareth Latty Avatar answered Oct 17 '22 19:10

Gareth Latty


Python is a dynamically typed language, which means that the "get" function does not declare its return type. When you're entering code in IPython or in the PyCharm console, the code is actually being executed, and it's possible to inspect the object instance in the running interpreter and to get the list of its methods. When you're entering code in PyCharm or in any other Python IDE, it is not executed, and it's only possible to use static analysis to infer the return type of the method. This is not possible in all cases.

like image 8
yole Avatar answered Oct 17 '22 20:10

yole


PyCharm has no idea what the dict contains if you fill it dynamically. So you have to hint PyCharm about the keys of dict beforehand. Prodict does exactly this to hint PyCharm, so you get code completion.

First, if you want to be able to access the response object, then you have to get a json response and convert it to dict. That's achieved with .json() method of requests like this:

response = requests.get("https://some.restservice.com/user/1").json()

OK, we loaded it to a dict object, now you can access keys with bracket syntax:

print(response['name'])

Since you ask for auto code completion, you certainly need to hint PyCharm about the keys of dict. If you already know the respone schema, you can use Prodict to hint 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, both name and price attributes are auto-complated.

If you don't know the schema of the response, then 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 since PyCharm can't know what the schema is.

What's more is, Prodict class is derived directly from dict, so you can use it as dict too.

This is the screenshot from Prodict repo that illustrates code completion:

Prodict code completion

Disclaimer: I am the author of Prodict.

like image 1
Ramazan Polat Avatar answered Oct 17 '22 19:10

Ramazan Polat