Search code examples
pythonvisual-studio-codedocstring

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.

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.


Solution

  • 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