gpt4 book ai didi

python - sphinx文档中如何自动添加参数类型

转载 作者:行者123 更新时间:2023-12-02 06:34:13 25 4
gpt4 key购买 nike

我目前正在尝试使用 Sphinx 实现自动文档创建(使用扩展 sphinx-apidoc 和 napoleon)。这工作得很好,但如果类型提示(PEP484 约定)自动添加到参数列表中会更好。

我想知道这是否可能。

更具体地说:(来自 napoleon example )

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.

"""

渲染如下:

enter image description here

参数列表包含所有参数,但不附加类型。可以手动添加它们,但这可能会在决定更改签名时引入 future 的问题。

手动添加类型的示例:

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.

"""

呈现为:

enter image description here

最佳答案

您现在可以使用sphinx-autodoc-typehints扩大。当您在上面的前一个示例中编写时,它会自动将类型添加到 sphinx 文档字符串中。

要安装,只需执行以下操作:

$ pip install sphinx-autodoc-typehints

'sphinx_autodoc_typehints'添加到conf.py中的扩展列表中'sphinx之后。 ext.napoleon',并确保您还将 napoleon_use_param = True 添加到 conf.py

关于python - sphinx文档中如何自动添加参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com