Documenting `tuple` return type in a function docstring for PyCharm type hinting

穿精又带淫゛_ 提交于 2019-12-30 03:44:33

问题


How can I document that a function returns a tuple in such a way that PyCharm will be able to use it for type hinting?

Contrived example:

def fetch_abbrev_customer_info(customer_id):
  """Pulls abbreviated customer data from the database for the Customer
       with the specified PK value.

       :type customer_id:int The ID of the Customer record to fetch.

       :rtype:???
  """
  ... magic happens here ...

  return customer_obj.fullname, customer_obj.status #, etc.

回答1:


I contacted PyCharm support, and this is what they said:

For tuple please use (<type_1>, <type_2>, <type_3>, e t.c.) syntax.

E.g.:

"""
:rtype: (string, int, int)
"""

This is confirmed in PyCharm's documentation:

Type Syntax

Type syntax in Python docstrings is not defined by any standard. Thus, PyCharm suggests the following notation:

...

  • (Foo, Bar) # Tuple of Foo and Bar


来源:https://stackoverflow.com/questions/18090037/documenting-tuple-return-type-in-a-function-docstring-for-pycharm-type-hinting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!