I'm using PyCharm
as editor. For example, I have next function:
def get_instance():
# method without doc sctring in some module
# returns instance of MyClass
return some_var
And I have second file which calls get_instance()
:
var = get_instance()
How I can to define type of data for value? I want to use autocomplete for variable.
For example in php
it will be like this:
/** @var MyClass $var */
$var = getInstance();
After this I will be see all methods and properties of $var
. How to declare docstring for python variable? I know about :type
and :rtype
, but if I need declare type for specific variable? Can I declare a few types? What I should do?
Note: I can't do any changes with get_instance()
.
You can do it in the same way as description properties of class. Here example:
from example_module import MyClass
var = get_instance()
"""
:type var: MyClass
"""
Also you can use description without import like this:
var = get_instance()
"""
:type var: example_module.MyClass
"""
The second way is using PEP 484:
test = my() # type: dict
Examples of autocomplete in Pycharm: