problems about specifying generic return type in PyCharm + python2.7

Completed

How to define the docstring in following function?

``` python
def get_object(klass, **kwargs):
"""
:rtype: ???
"""
# perform some tasks
return klass(**kwargs)
```

I've tried `klass`, `type(klass)`, `klass.__class__`,
they all didn't work in separated module files.

``` python
from sample.utils import get_object
from sample.models import User

u = get_object(User, name='test')
u. # no PyCharm hint here
```

1
2 comments

according to http://stackoverflow.com/questions/43074829/how-to-specifying-generic-return-type-in-pycharm-python2-7

 

the docstring should be:

"""

:type klass: () -> T

:rtype: T

"""

1

Pycharm tends to be pretty good about auto determining the type from get_object() as long as klass() has sufficient type hint.  (at least the last time I played with 2.7 using 2017.1).  This assumes you own klass to be able to set the type hint.

If not, @Lllki is correct.  You can set the type hint in the docstring.

0

Please sign in to leave a comment.