python - 将函数签名与文档字符串进行比较的工具

标签 python visual-studio-code signature docstring code-documentation

是否有工具可以检查文档字符串中列出的参数是否与函数调用的签名匹配?它应该能够处理 numpy 风格的文档字符串。
我经常使用 R CMD CHECK,它会发现 R 中的文档/代码不匹配,这非常有帮助。在 Python 中有类似的东西会很好,但我还没有找到任何东西。

最佳答案

我刚刚创建了一个工具来实现这一点,名为 pydoctest .
它将尝试推断您的文档字符串中的类型(不仅仅是词法比较)并报告参数数量、参数名称、参数类型、返回类型之间的不匹配,(可选)如果缺少文档字符串则抛出错误等。
它目前支持 google、sphinx 和 numpy docstring 格式,但可以很容易地使用其他格式进行扩展。
例子:

def func_type_mismatch(self, a: int) -> int:
    """[summary]

    Args:
        a (float): [description]        <-- float is not int

    Returns:
        int: [description]
    """
    pass
在这个函数上运行 pydoctest,给出这个输出:Function: <function IncorrectTestClass.func_type_mismatch at 0x7f9a8b52c8c8> FAIL | Argument type differ. Argument 'a' was expected (from signature) to have type '<class 'int'>', but has (in docs) type '<class 'float'>'编辑(2021 年 6 月):我已经开始开发使用并突出显示错误的 vscode 扩展。
enter image description here
https://marketplace.visualstudio.com/items?itemName=JeppeRask.pydoctest

关于python - 将函数签名与文档字符串进行比较的工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63709432/

相关文章:

python - 如何在 Pandas 中读取奇怪的 csv 文件?

python - 如何使用plotly imshow设置xlim/ylim?

python - python中的低通线性滤波器

ubuntu - 努力使用 gettng Visual Studio Code 在 Ubuntu 20.04 中显示 Console.Writeline()

c++ - 如何在 C++ 中重载 std::function 签名

Python检查目录中具有特定名称的所有文件

c++ - Visual Studio Code 在终端中运行程序以进行输入

visual-studio-code - 如何防止 Visual Studio Code 中 'Replace all' 之后自动保存文件?

excel - 从 Excel VBA 将签名插入 Outlook 电子邮件

c# - C# 中 MVC DocuSign WebHook 接收器的正确类是什么