Python 3:Sphinx 没有正确显示类型提示
Python 3: Sphinx doesn't show type hints correctly
我正在使用 Sphinx (make HTML) 从其函数的 reStructuredText 文档字符串自动生成 HTML 文档 Python 3 模块。到目前为止,生成的 HTML 文档看起来不错,但是函数签名的参数类型(在源代码中作为 PEP484 type hints 给出)未正确显示。
例如这是我的一个函数的 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
这是我期望的样子:
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
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 []
如何让 Sphinx 正确显示 Python 3 种类型提示?
自行解决了这个问题
有autodoc_typehints
个变量。从 3.0 版开始,您可以将其设置为 'description'
,这会将类型提示显示为函数或方法的内容,并将它们从签名中删除。
所以只需将此行添加到您的 conf.py
:
autodoc_typehints = "description"
我正在使用 Sphinx (make HTML) 从其函数的 reStructuredText 文档字符串自动生成 HTML 文档 Python 3 模块。到目前为止,生成的 HTML 文档看起来不错,但是函数签名的参数类型(在源代码中作为 PEP484 type hints 给出)未正确显示。
例如这是我的一个函数的 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
这是我期望的样子:
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
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 []
如何让 Sphinx 正确显示 Python 3 种类型提示?
有autodoc_typehints
个变量。从 3.0 版开始,您可以将其设置为 'description'
,这会将类型提示显示为函数或方法的内容,并将它们从签名中删除。
所以只需将此行添加到您的 conf.py
:
autodoc_typehints = "description"