在 PyCharm 类型提示的函数文档字符串中记录 `tuple` 返回类型

编程入门 行业动态 更新时间:2024-10-23 09:26:05
本文介绍了在 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` 返回类型

本文发布于:2023-10-16 11:59:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497495.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   字符串   函数   提示   文档

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!