在 PyCharm 类型提示的函数文档字符串中记录 `tuple` 返回类型 [英] Documenting `tuple` return type in a function docstring for PyCharm type hinting

查看:70
本文介绍了在 PyCharm 类型提示的函数文档字符串中记录 `tuple` 返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

人为的例子:

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.

推荐答案

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

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

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

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

例如:

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

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

This is confirmed in PyCharm's documentation:

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

Type Syntax

Type syntax in Python docstrings is not defined by any standard. Thus, PyCharm suggests the following notation:

...

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

这篇关于在 PyCharm 类型提示的函数文档字符串中记录 `tuple` 返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆