vytaute
vytaute

Reputation: 1480

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?

Upvotes: 5

Views: 2845

Answers (3)

plasma
plasma

Reputation: 297

Add --ignore=E302,E265 to python.formatting.autopep8Args can also be an alternative way to ignore the extra two lines between the outer layer comment and code. The downside is that it would not correct the right number of lines between two functions anymore.

similar question here

Upvotes: 0

Christopher
Christopher

Reputation: 359

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

Upvotes: 3

user8563312
user8563312

Reputation:

This is due to the code convention (PEP8) of Python. Pylance will correct your code when saving.
This is an excerpt of PEP8 which you can find here:
You should use two spaces after a sentence-ending period.

If the comment describes your function, try using Docstrings.

Upvotes: 1

Related Questions