在函数docstring中记录`tuple`返回类型,用于PyCharm类型提示

33 python docstring pycharm

我如何记录函数返回的tuple方式,使PyCharm能够将其用于类型提示?

举例:

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.
Run Code Online (Sandbox Code Playgroud)

小智 44

我联系了PyCharm的支持,这就是他们所说的:

对于元组,请使用(<type_1>, <type_2>, <type_3>, e t.c.)语法.

例如:

"""
:rtype: (string, int, int)
"""
Run Code Online (Sandbox Code Playgroud)

这在PyCharm的文档中得到了证实:

键入语法

Python文档字符串中的类型语法不是由任何标准定义的.因此,PyCharm建议使用以下符号:

...

  • (Foo,Bar)#Foo和Bar的Tuple