简体   繁体   English

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

[英]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? 如何记录函数以PyCharm能够将其用于类型提示的方式返回tuple

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: 我联系了PyCharm的支持,这就是他们所说的:

For tuple please use (<type_1>, <type_2>, <type_3>, e tc) syntax. 对于元组,请使用(<type_1>, <type_2>, <type_3>, e tc)语法。

Eg: 例如:

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

This is confirmed in PyCharm's documentation : 这在PyCharm的文档中得到了证实:

Type Syntax 键入语法

Type syntax in Python docstrings is not defined by any standard. Python文档字符串中的类型语法不是由任何标准定义的。 Thus, PyCharm suggests the following notation: 因此,PyCharm建议使用以下符号:

... ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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