Mark Harrison Mark Harrison - 7 months ago 46
Python Question

PyCharm: specifying type of returned variable?

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


enter image description here

Python 2,7, PyCharm Community Edition 2016.1.2

Answer

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)

enter image description here


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

enter image description here

Comments