In VS Code, how can I change the problem level of a certain error/violation from the Flake8 extension?

199 Views Asked by At

I'm using VS Code with Flake8 configured to check some obvious issues for my Python code. However, some of its errors are not really errors in my current development phase, e.g. E501 (line too long) and F401 (unused import).

The annoying thing is that the relevant lines are marked in red, which makes more serious errors non-obvious.

Is there a way I can tell Flake8 to treat them as warnings instead of errors?

I searched all over, but only discovered ways to either ignore the check all together, or explicitly mark one line as ignored, e.g. How to tell flake8 to ignore comments. They are not what I need.

2

There are 2 best solutions below

6
On BEST ANSWER

Use the flake8.severity setting contributed by the Flake8 extension. See https://github.com/microsoft/vscode-flake8#settings.

Quoting from the docs, the default value is:

{
  "convention": "Information",
  "error": "Error",
  "fatal": "Error",
  "refactor": "Hint",
  "warning": "Warning",
  "info": "Information"
}

The description in the docs is:

Controls mapping of severity from flake8 to VS Code severity when displaying in the problems window. You can override specific flake8 error codes

{
  "convention": "Information",
  "error": "Error",
  "fatal": "Error",
  "refactor": "Hint",
  "warning": "Warning",
  "W0611": "Error",
  "undefined-variable": "Warning"
}

Pay particular attention to the property W0611 in that example above. See also https://flake8.pycqa.org/en/latest/user/error-codes.html.

Since Flake8 uses other plugins, you'll need to refer to the readmes and docs of those plugins to find out what error codes they provide (I think Flake8 mostly just uses them without any sort of translation):

6
On

Create a tox.ini or .flake8 file in your project, and add your exceptions there. Flake8 should pick up on that.

From those docs:

[flake8]
# it's not a bug that we aren't using all of hacking, ignore:
# H101: Use TODO(NAME)
# H202: assertRaises Exception too broad
# H233: Python 3.x incompatible use of print operator
# H301: one import per line
# H306: imports not in alphabetical order (time, os)
# H401: docstring should not start with a space
# H403: multi line docstrings should end on a new line
# H404: multi line docstring should start without a leading new line
# H405: multi line docstring summary not separated with an empty line
# H501: Do not use self.__dict__ for string formatting
extend-ignore = H101,H202,H233,H301,H306,H401,H403,H404,H405,H501

And now Flake8, and thus VS Code, won't flag any of those.