Search code examples
pythonvisual-studio-codeformattingpylance

Auto save adds two empty lines between comment and function header in Python in VS code


I write code in Python in VS code. If I add comment before function and hit save button, VS code adds two empty lines:

# comment


def MyMethod():
    return 0

In settings I see that I use autopep8 formatter: autopep8 formatter

I wasnt able to find what causes this annoying issue. Maybe I can configure settings somewhere?


Solution

  • Comment documenting function behavior should be placed inside function, just below signature. If it is not describing function (is placed outside function) it should have those blank lines. Don't mess with language's conventions, it's very bad idea.

    Edit, Further clarification:

    """Module-level comment
    """
    
    
    def function(): 
        """Function level comment. 
        There are multiple conventions explaining how
        a comment's body should be formed.
        """
        return 0