在函数docstring中记录PyCharm类型提示的“tuple”返回类型

2024-10-02 10:27:53 发布

您现在位置:Python中文网/ 问答频道 /正文

如何记录函数返回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.

Tags: the函数idobj类型def方式记录
1条回答
网友
1楼 · 发布于 2024-10-02 10:27:53

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

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

E.g.:

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

这在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

相关问题 更多 >

    热门问题