python - 如何让 PyC​​harm 从函数定义中获取类型提示并在文档字符串中填充类型值?

标签 python types pycharm docstring

我总是在函数定义中使用类型提示,例如:

def foo(a: int, b: str) -> bool:
    pass

当我使用 PyCharm 自动文档字符串生成器在我的代码中生成文档字符串时,我得到了这个:
def foo(a: int, b: str) -> bool:
    """
    :param a: 
    :type a: 
    :param b: 
    :type b: 
    """
    pass

如您所见,我在函数本身中定义的类型值没有被 PyCharm 识别,我应该再次将它们写在 docstring 中。我如何让 PyC​​harm 为我自动生成这样的东西(从第一行读取类型值并将它们插入到文档字符串中):
def foo(a: int, b: str) -> bool:
    """
    :param a:
    :type a: int
    :param b:
    :type b: str
    :rtype: bool
    """
    pass

最佳答案

PyCharm Bugtracker 上有一个功能请求:Generate docstring types based on the existed inline annotations
截至 2020 年底,PyCharm 似乎仍然不支持将方法中的类型信息自动添加到文档字符串中。
功能请求中有一条评论,询问为什么需要这样的功能:

A question to everyone following, why do you need to put type hints both in annotations and docstrings? The latter format is only partially supported by PyCharm (not every possible PEP 484 type hint is understood there, see PY-23400) and not recognized at all by most other type checkers. Type hints in annotations, on the other hand, are already properly displayed in Quick Documentation and in the documentation rendered by Sphinx (perhaps with the help of sphinx-autodoc-typehints).

关于python - 如何让 PyC​​harm 从函数定义中获取类型提示并在文档字符串中填充类型值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56399298/

相关文章:

python - 在 Pandas Python 中对 DF 进行子集化

python - 在 python 列表中保留最常出现的值

python - Pandas 日期范围到单个日期

java - SWIGTPYE_p 数据类型 Java

virtualenv - PyCharm - 自动设置环境变量

python - 如何在 Python 中连接到 Intersys 缓存数据库

python - 使用 pymongo tailable 游标死于空集合

vb.net - 为什么使用 Type.Equals(t1, t2) 而不是相等运算符?

sql - 为 varchar 或 varbinary 选择较大 MAX 值的缺点

html - Flask Pycharm css 文件永久附加到 html