Why does my python formatter add blank lines before and after function definitions?

1.1k Views Asked by At

My code is automatically formatted by the formatter, and it adds blank lines that I do not want. This happens while using both autopep8 and black. This is my code:

dict_n = {}
def new_dash():
    new_list = input("do something ").split(",")
    for i in range(len(new_list)):
        # dict_n[new_list[i]] = [i]
        dict_n[i] = new_list[i]
    print(dict_n)
new_dash()

which gets formatted to:

dict_n = {}


def new_dash():
    new_list = input("do something ").split(",")
    for i in range(len(new_list)):
        # dict_n[new_list[i]] = [i]
        dict_n[i] = new_list[i]
    print(dict_n)


new_dash()

Is there a way I can prevent the extra lines being added automatically by the formatter, possibly by editing the settings.json file in VSCode?

1

There are 1 best solutions below

1
JialeDu On

First of all, this is a formatting standard, for the sake of code specification and code readability, etc. Generally, it is not recommended to change it.

If you really want to change, the following is a solution, but it can only remove the blank lines above. To add the following settings in settings.json,

    "python.formatting.provider": "black",
    "python.formatting.blackArgs": [
        "--skip-source-first-line",
    ],

enter image description here

See more about this settings here.