Type hinting list of objects

Answered

I've used phpstorm a fair bit and know that you can specify a variable's type by doing @var .....

In pycharm the only way I've found to do that is to assert the variable

for item in self.get_contact_lists():
assert isinstance(item, models.ContactList)
....

Which is great and all, but I'm hoping there is someway I could just type hint in the get_contacts_list method what is being returned.

7 comments
Comment actions Permalink

I should mention that I have been looking online for solutions and so far what I've found and tried do not seem to work as I'm expecting in the latest version.

http://stackoverflow.com/questions/17824280/how-to-specify-that-a-parameter-is-a-list-of-specific-objects-in-python-docstrin

#: :type: list of models.ContactList

That is one I've tried without any luck, there are others I've tried over the last few months but so far no luck.

0
Comment actions Permalink

Does this help you? https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html

You can specify the return type from the method using

:rtype: object

0
Comment actions Permalink

The function is returning a list of objects, not a single object. :rtype: doesn't work for objects within the list itself..

0
Comment actions Permalink

Hi Ian. Sorry for the late reply.

Actually, there are multiple ways to type hint your code in PyCharm (especially, regarding function signatures).

First of all, you can use docstrings, as Marcel suggested earlier. You can write it in whatever docstring format you like. For instance, in reStructuredText

def get_contact_lists(self):
"""
:rtype: collections.Iterable[models.ContactList]
"""
return undefined()

or in Google Python Style (Napoleon) docstrings flavor

def get_contact_lists(self):
"""
Returns:
collections.Iterable[models.ContactList]
"""
return undefined()


The exact syntax of type annotations in docstring that we support is described in details in our python-skeletons project.

Meanwhile, the standard way of adding optional type annotations has emerged in Python community with appearance of PEP 484 and typing module. Unlike, syntax of python-skeletons these annotations are recognized not only by PyCharm, but also in other tools like mypy and pytype.

In Python 3 you can use standard syntax for annotating return value of function introduced back in PEP 3107

from typing import Iterable
from models import ContactList

class Contacts:
def get_contact_lists(self) -> Iterable[ContactList]:
return undefined()

And in Python 2/3 compatible code you can use the alternative approach with special "#type:" comments:

from typing import Iterable
from models import ContactList

class Contacts:
def get_contact_lists(self):
# type: () -> Iterable[ContactList]
return undefined()

 

1
Comment actions Permalink

Perfect, thanks that helps a lot.

0
Comment actions Permalink

I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.

python training in chennai

0

Please sign in to leave a comment.