9

I'm using the flake8 linter for Python and I have many code formats issues like blank line contains whitespace flake8(W293)

I'm trying to auto fix these linting issues. I have these settings:

    "python.linting.enabled": true,
    "python.linting.flake8Enabled": true,
    "python.linting.lintOnSave": true,
    "python.linting.flake8Args": [
        "--ignore=E501",
    ],
    "editor.formatOnSave": true

I'm using the default autopep8 formater but it seems that it does nothing. Nothing happens when I save the file or run the command Format Document.

Is there any way to auto fix these linting errors?

nrofis
  • 8,975
  • 14
  • 58
  • 113

2 Answers2

8

I would suggest using a formatter, black for instance, to fix the issues detected by your linter.

If so, pip install it and add this to your settings.json:

"python.formatting.provider": "black"

Then, pressing Alt+ShifT+F or Ctrl+S should trigger the formatting of your script.

030
  • 10,842
  • 12
  • 78
  • 123
Laurent
  • 12,287
  • 7
  • 21
  • 37
  • It workes! But I just not understand why the default `autopep8` isn't working... – nrofis Sep 11 '21 at 12:15
  • 1
    Could have something to with how you installed it at first. More on this: https://stackoverflow.com/q/49783700/11246056 – Laurent Sep 11 '21 at 12:27
2

It should be:

 "python.linting.flake8Args": ["--ignore=W293"],

And you can switch the formatting to yapf or black.

If you insist on autopep8, you can add this in the settings.json:

"python.formatting.autopep8Args": ["--select=W293"],
Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13