16

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?

8 Answers 8

23

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.

3
  • If this feature does what I think it does ( collecting information about types that a language like python inherently doesn't offer and then help complete methods while coding) then PyCharm has, just with this, beaten any other IDE I know. It's all the goodness of typed languages to get IDE feedback while still allowing all the cool things of loosely typed languages. Apr 10, 2018 at 9:18
  • 1
    Updated location for PyCharm 2018.3: File > Settings > Build, Execution, Deployment > Python Debugger
    – OfirD
    Apr 23, 2019 at 19:50
  • @HeyJude Thanks for the note. I have updated the post to show the new location. Apr 24, 2019 at 1:40
8

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.

1
  • 1
    I am from the Java world and new to Python. I am using PyCharm and was writing my first application where I used Amazon's boto3 library. As soon as I got an object from a method of that library I was forced to look at the documentation in order to identify its properties and methods (as the PyCharm is not recommending anything). Really annoying... How do you debug in python? I was always running the code to identify "attribute not existing" errors. Dec 28, 2019 at 21:11
1

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.

0

if will just detect methods or variables and... with write some part of it: File->Setting -> Editor -> General -> Code Completion in top of opened window , unCheck [ Mach Case ]

0

It's an old question but probably all the provided answers missed the mark by a margin as wide as Sun's distance to Betelgeuse (none of the answers is accepted and @user1265125 is an active guy with 8 yrs here and more cred than me).

As it happens, I've just had exactly the same problem as OP and the solution was:

A NON-ASCII CHAR SOMEWHERE IN THE PROJECT'S FOLDER PATH

Seriously, PyCharm devs...[doubleFacepalm]

0

In my case the solution is to reset the settings, everething else wasn`t working for me. "From the main menu, select File | Manage IDE Settings | Restore Default Settings.Alternatively, press Shift twice and type Restore default settings."

0

I had a similar problem. Only functions I had already used were suggested and only as plain text and not recognised as methods.

What fixed that for me was deleting the /.idea folder in the project directory. (Afterwards you will have to set your run configurations again)

0

With the latest version update to 2022.2, even auto-complete stopped working for me. After quite a bit of reading articles, I just found the https://youtrack.jetbrains.com/issue/PY-50489 issue which was the root problem. The old plugins were pending update, after that, the code completion issue was fixed.

So, try and check if you are facing the same problem, if the plugins are up to date in Settings —> Plugins.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.