gpt4 book ai didi

python - 验证代码和文档匹配

转载 作者:行者123 更新时间:2023-11-28 19:10:28 26 4
gpt4 key购买 nike

使用 Sphinx 扩展 NumpyDoc,是否有某种方法可以自动确保文档与其记录的代码匹配?

例如,由于拼写错误,以下文档与代码不匹配:

def myfunc(apples, bears):
"""
Parameters
----------
apples : int
The number of apples.
beards : int
The number of bears to eat the apples.
"""

Sphinx 或 NumpyDoc 能使这个错误吗?

最佳答案

这是内置在 NumpyDoc 或 Sphinx 中的,但可以使用 NumpyDoc's scraping abilities .以下是实现所需功能的代码片段:

import inspect

from numpydoc.docscrape import FunctionDoc

def myfunc(apples, bears):
"""
Parameters
----------
apples : int
The number of apples.
beards : int
The number of bears to eat the apples.
"""

doc = FunctionDoc(myfunc)
argspec = inspect.getargspec(myfunc)

# check for basic spelling errors
for a_i, arg in enumerate(argspec.args):
if arg != doc["Parameters"][a_i][0]:
print("%s != %s" %(arg, doc["Parameters"][a_i][0]))

关于python - 验证代码和文档匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071661/

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