简体   繁体   中英

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

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.

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

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

Eg:

 """ :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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM