Reputation: 304434
How do I make PyCharm understand completions for a returned variable?
As per https://www.jetbrains.com/help/pycharm/5.0/type-hinting-in-pycharm.html
I'm asserting the type of a variable, but not getting any completions.
import boto.kinesis
x = boto.kinesis.connect_to_region("us-west-2")
assert isinstance(x, boto.kinesis.layer1.KinesisConnection)
foo = x. <--- not getting completions here
Python 2,7, PyCharm Community Edition 2016.1.2
Upvotes: 1
Views: 270
Reputation: 304434
(PyCharm Developer replies, via JetBrains support)
As @alexce noted the underlying problem is that PyCharm fails to resolve boto.kinesis.layer1
reference and hence symbol to boto.kinesis.layer1.KinesisConnection
can't be resolved too.
Normally you can't refer to submodules of a package this way (without importing them explicitly). If PyCharm allowed it, there would be tons of false negative errors when one forgets to add necessary import statement. But because situations like that are quite common, we look through boto/kinesis/__init__.py
trying to find some indication that sys.modules['boto.kinesis']
might already contain the attribute layer1
.
Anything of the following would do:
from boto.kinesis import layer1
from . import layer1
from boto.kinesis.layer1 import KinesisConnection
import boto.kinesis.layer1
'layer1' in __all__
In fact __init__.py
indeed contains the third of these import statements, but inside a function:
def regions():
...
from boto.kinesis.layer1 import KinesisConnection
...
Since we can't guarantee statically that this function is called at runtime, we have nothing more to do other than warn about the unresolved reference in the editor.
Upvotes: 1
Reputation: 473763
This is related to Cannot find reference 'xxx' in __init__.py - Python / Pycharm and the fact that there is no __all__
defined inside the __init__.py
inside the boto.kinesis
package.
Importing the KinesisConnection
directly from boto.kinesis.layer1
made it work for me:
import boto.kinesis
from boto.kinesis.layer1 import KinesisConnection
x = boto.kinesis.connect_to_region("us-west-2")
assert isinstance(x, KinesisConnection)
You can also import it and add a type
hint into the comments:
import boto.kinesis
from boto.kinesis.layer1 import KinesisConnection
x = boto.kinesis.connect_to_region("us-west-2") # type: KinesisConnection
Upvotes: 1