可以在python 3中输入提示用于生成docstring吗?

Jin*_*hoi 8 python python-3.x

我们可以在python中使用docstring指示函数参数的类型:

def f1(a):
    """
    :param a: an input.
    :type a: int
    :return: the input integer.
    :rtype: int
    """
    return a
Run Code Online (Sandbox Code Playgroud)

对于f1,autodoc生成以下文档:

fun1(a)
    Parameters : a (int) – an input.
    Returns    : the input integer.
    Return type: int
Run Code Online (Sandbox Code Playgroud)

在python 3中,类型也可以通过类型提示来表示:

def f2(a: int):
    """
    :param a: an input.
    :return: the input integer.
    :rtype: int
    """
    return a
Run Code Online (Sandbox Code Playgroud)

当我们运行autodoc时,它通过参数声明放置类型,但不在描述中:

f2(a: int)
    Parameters : a – an input.
    Returns    : the input integer.
    Return type: int
Run Code Online (Sandbox Code Playgroud)

是否可以f1使用注释而不是docstring 生成文档?我正在使用python 3.6.谢谢!

Jim*_*ard 4

还没有,据我所知 Sphinx 还不支持这一点。评论中引用的错误与类型提示的表示有关,而不是它们的位置。

我确实知道 Sphinx 目前有一个扩展可以为您处理这个问题,称为sphinx-autodoc-typehints。您可能暂时可以使用它。