当函数体中存在多行字符串时,无法在 VS Code 中生成带有 autoDocstring 扩展的 Python 文档字符串

发布于 2025-01-09 02:43:02 字数 936 浏览 1 评论 0原文

要使用 Python Sphinx 生成文档,我必须使用特定的文档字符串格式。

VS Code 扩展 autoDocstring 能够生成这种特定格式,但如果函数包含多行字符串,则它不起作用。

这种情况下的示例有效:

def func(param1, param2, param3):
    # docstring nicely generated
    """_summary_

    :param param1: _description_
    :type param1: _type_
    :param param2: _description_
    :type param2: _type_
    :param param3: _description_
    :type param3: _type_
    :return: _description_
    :rtype: _type_
    """

    random_variable = 42
    string_variable = "not a multiline string"

    return string_variable

但在这种情况下无法生成自动文档字符串:

def func(param1, param2, param3):
    # doesn't work
    """"""

    random_variable = 42
    string_variable = """
             a 
             multiline
             string
     """

    return string_variable

任何人都知道一个技巧,或者使它起作用的东西吗? 我在函数中使用了很多多行 SQL 字符串,如果我必须提取这些字符串才能使其正常工作,那么我需要进行大量重构。

To generate documentation with Python Sphinx I have to use a specific docstring format.

VS Code extension autoDocstring is capable to generate this specific format, but if the function contains multiline string then it doesn't work.

Example in this case works:

def func(param1, param2, param3):
    # docstring nicely generated
    """_summary_

    :param param1: _description_
    :type param1: _type_
    :param param2: _description_
    :type param2: _type_
    :param param3: _description_
    :type param3: _type_
    :return: _description_
    :rtype: _type_
    """

    random_variable = 42
    string_variable = "not a multiline string"

    return string_variable

But in this case can't generate auto docstring:

def func(param1, param2, param3):
    # doesn't work
    """"""

    random_variable = 42
    string_variable = """
             a 
             multiline
             string
     """

    return string_variable

Anyone know a trick, or something to make it work?
I use a lot of multiline SQL strings in my functions and if I have to extract these strings just to make it work I need a lot of refactoring.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友谊不毕业 2025-01-16 02:43:03

键盘快捷键:ctrl+shift+2cmd+shift+2(适用于 Mac)

Keyboard shortcut: ctrl+shift+2 or cmd+shift+2 for mac

终难愈 2025-01-16 02:43:03

我找到了解决方案,并将其发布在这里,也许会对某人有所帮助。
实际上,解决方案非常简单。
我将函数/类/任何字符串变量中的三个撇号更改为三个单引号,现在 autoDocstring 的解析器不会感到困惑。
例子:

def func(param1, param2, param3):
    # autoDocstring works
    """_summary_

    :param param1: _description_
    :type param1: _type_
    :param param2: _description_
    :type param2: _type_
    :param param3: _description_
    :type param3: _type_
    :return: _description_
    :rtype: _type_
    """

    random_variable = 42
    string_variable = '''
             a 
             multiline
             string
     '''

    return string_variable

I figured out the solution and I post it here, maybe will help somebody.
Actually the solution is pretty straightforward.
I changed the triple apostrophes to triple single quotes in the function/class/whatever string variable and now autoDocstring's parser doesn't get confused.
Example:

def func(param1, param2, param3):
    # autoDocstring works
    """_summary_

    :param param1: _description_
    :type param1: _type_
    :param param2: _description_
    :type param2: _type_
    :param param3: _description_
    :type param3: _type_
    :return: _description_
    :rtype: _type_
    """

    random_variable = 42
    string_variable = '''
             a 
             multiline
             string
     '''

    return string_variable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文