简体   繁体   English

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

[英]Can't generate Python docstring with autoDocstring extension in VS Code when multiline string in the function body

To generate documentation with Python Sphinx I have to use a specific docstring format.要使用 Python Sphinx 生成文档,我必须使用特定的文档字符串格式。

VS Code extension autoDocstring is capable to generate this specific format, but if the function contains multiline string then it doesn't work. VS Code 扩展autoDocstring能够生成此特定格式,但如果 function 包含多行字符串,则它不起作用。

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.我在我的函数中使用了很多多行 SQL 字符串,如果我必须提取这些字符串才能使其正常工作,我需要进行大量重构。

键盘快捷键: ctrl+shift+2cmd+shift+2 for mac

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.我将函数/类/任何字符串变量中的三重撇号更改为三重单引号,现在 autoDocstring 的解析器不会混淆。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM