3
votes

I have the following file. Why does code completion not run when I press Ctrl-Space after the "r."? It says "no suggestion" in a red box.

(The program as it is runs and puts out: 200)

__author__ = 'hape'

import urllib.request
import urllib.response

print("Starting")
r = urllib.request.urlopen("http://www.python.org")

r.  <------------ No code completion, why not?!

print (r.getcode())

After the r., code completion does not popup, why?

4
PyCharm doesn't know the type of r as the library has no type hints for urlopen. - CrazyCoder
@CrazyCoder has the answer here. A quick and dirty solution to the issue is to put an import pdb; pdb.set_trace() at the line in question, and in the debugger terminal enter dir(r) to reveal the attributes of the r object. - EML
thank You both very much for the hints. - Hartmut P.
PyCharm 2.7 will use runtime information from the debugger for type hints. - CrazyCoder

4 Answers

3
votes

Adding response from JetBrains: @CrazyCoder was right there. The problem is that we are not able to infer proper return type of the function "urllib.request.urlopen()" since its implementation uses some dynamic tricks that we cannot handle statically, in particular:

Normally, we deal with difficult cases like that using external annotations in python-skeletons but it doesn't contain type hints for "urllib.request" module yet. Also in the upcoming versions of PyCharm we're planning to switch to the collection of annotations gathered in typeshed project. It evolves much more actively and already contains some annotations for "urllib". To benefit from them you just need to drop "urllib" package with annotations somewhere in your interpreter paths, so that PyCharm could find the respective .pyi stubs.

Screen Shot from JB

1
votes

Check whether the IDE is in Power Saving Mode. If it is, then no code completion process or any any other background process works

It shows about it in the status bar at the bottom of the IDE

0
votes

@CrazyCoder was right.Fow now, Pycharm does not kown the type of r.

If you really like to auto completion, first get the type of r using IPython or debug

# IPython

In [1]: import urllib.request

In [2]: r = urllib.request.urlopen("http://www.python.org")

In [3]: type(r)
Out[3]: http.client.HTTPResponse

then use Python3 Annotations

r: http.client.HTTPResponse = urllib.request.urlopen("http://www.python.org")
r.  

Now, you can get

python annotation for http.client.HTTPResponse