Python 3:Sphinx没有正确显示类型提示

Ger*_*rer 8 python-sphinx python-3.5

我正在使用Sphinx(make HTML)从其函数的reStructuredText文档字符串自动生成Python 3模块的HTML文档.到目前为止,生成的HTML文档看起来很好,但函数签名的参数类型(在源代码中作为PEP484类型提示给出)未正确显示.

例如,这是我的一个函数的Sphinx生成的HTML文档的一些示例输出:

static parse_from_file(filename: str) ? list
    Parses stuff from a text file.

    Parameters:  filename – the filepath of a textfile to be parsed
    Returns:     list of parsed elements
Run Code Online (Sandbox Code Playgroud)

这就是我期望的样子:

static parse_from_file(filename)
    Parses stuff from a text file.

    Parameters:  filename (str) – the filepath of a textfile to be parsed
    Returns:     list of parsed elements
    Return type: list
Run Code Online (Sandbox Code Playgroud)

这就是Python代码的实际外观:

def parse_from_file(filename: str) -> list:
    """Parses stuff from a text file.

    :param filename: the filepath of a textfile to be parsed
    :returns: list of parsed elements
    """
    return []
Run Code Online (Sandbox Code Playgroud)

如何让Sphinx正确显示Python 3类型的提示?

Ger*_*rer 10

我通过使用sphinx-autodoc-typehints扩展来自己解决它.


Bře*_*jek 10

autodoc_typehints变数。从 3.0 版开始,您可以将其设置为将'description'类型提示显示为函数或方法的内容,并将它们从签名中删除。

所以只需将此行添加到您的conf.py

autodoc_typehints = "description"
Run Code Online (Sandbox Code Playgroud)