[英]How to automatically add parameter types in sphinx documentation
我目前正在尝试使用Sphinx实现自动文档创建(使用扩展名sphinx-apidoc和napoleon)。 这非常有效,但如果将类型提示(PEP484约定)自动添加到参数列表中会更好。
我想知道这是否可行。
更具体地说:(来自拿破仑的例子 )
def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
"""Example function with PEP 484 type annotations.
Args:
param1: The first parameter.
param2: The second parameter.
Returns:
The return value. True for success, False otherwise.
"""
这呈现如下:
参数列表包含所有参数,但不附加类型。 可以手动添加它们,但是当决定更改签名时,这可能会引入未来的问题。
添加手动类型的示例:
def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
"""Example function with PEP 484 type annotations.
Args:
param1 (int): The first parameter.
param2 (str): The second parameter.
Returns:
The return value. True for success, False otherwise.
"""
其呈现为:
您现在可以使用sphinx-autodoc-typehints扩展。 当您在上面的示例中进行编写时,它会自动将类型添加到sphinx文档字符串中。
要安装,只需执行:
$ pip install sphinx-autodoc-typehints
添加'sphinx_autodoc_typehints'
的extensions
列表中conf.py
后 'sphinx.ext.napoleon'
,并确保您还添加napoleon_use_param = True
到conf.py
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.