【问题标题】:Documenting `tuple` return type in a function docstring for PyCharm type hinting在 PyCharm 类型提示的函数文档字符串中记录 `tuple` 返回类型
【发布时间】:2013-08-06 20:40:01
【问题描述】:

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

【问题讨论】:

  • :returns:客户姓名:他的名字,客户状态:他的状态,客户的狗的名字:通常是Fido。
  • 整个字符串不能只是"""Return (name, status, etc)"""吗?其他一切似乎有点多余。除非 PK 值与函数期望的 customer_id 类型有关。
  • 更新了问题文本以减少主观性。

标签: python pycharm docstring


【解决方案1】:

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

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

例如:

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

这已在PyCharm's documentation 中得到确认:

类型语法

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

...

  • (Foo, Bar) # Foo 和 Bar 的元组

【讨论】:

  • 我认为这从 PyCharm 2017.1.5 开始就被打破了
猜你喜欢
  • 1970-01-01
  • 2015-11-23
  • 2021-10-15
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多