Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get flake8 to reliably ignore rules in VS Code?

Two things that annoy me. First is the warning Flake8 gives me when I type more than 80 characters on a line. Second is the warnings I get when I haven't yet used a module name that I imported. I've looked at all the documentation on using Flake8 in the terminal. No use.

flake8 --ignore=E402
flake8 --max-line-length=120

This doesn't work. At least VS Code doesn't show any effect.

like image 913
reka18 Avatar asked May 04 '18 14:05

reka18


People also ask

How do I hide a warning in VS Code?

To disable TypeScript warnings in VS Code, we can set a few options in our VS Code settings to disable them. { //... "typescript. validate. enable": false, "javascript.

How do I disable VS Code Linter?

To disable TSLint in VS Code, we can set the "typescript. validate. enable" setting to false in our VS Code settings. json file.


4 Answers

Add your arguments to your USER SETTINGS json file like this:

"python.linting.flake8Args": [
    "--max-line-length=120",
    "--ignore=E402,F841,F401,E302,E305",
],
like image 157
reka18 Avatar answered Oct 05 '22 19:10

reka18


note that flake8 uses

"python.linting.flake8Args": [

whereas black seems to use:

"python.formatting.blackArgs": [

if you're using both (or toggling) these settings maybe helpful:

    {
        "python.linting.pylintEnabled": false,
        "python.linting.flake8Enabled": true,
        "python.linting.enabled": true,
        "python.formatting.provider": "black",
        "python.formatting.blackArgs": [
            "--line-length",
            "120"
        ],
        
        "python.linting.flake8Args": [
            "--max-line-length=120",
            "--ignore=E402",
        ],
    
        "python.pythonPath": "venv/bin/python"
    }

like image 35
dcsan Avatar answered Oct 05 '22 21:10

dcsan


I ran into this problem recently. I ran into problems because I was setting the argument to --config flake8.cfg instead of --config=flake8.cfg. Under the hood, vscode puts the CLI argument in quotes. Adding "--config flake8.cfg" to the flake8 command seems to confuse flake8 into thinking that it's looking at a file path and not a CLI argument.

The solution for me was to either set the args as --config=flake8.cfg (with the equals sign) or the args up into separate items in the array:

"python.linting.flake8Args": [
  "--config",
  "flake8.cfg"
]
like image 38
David Newswanger Avatar answered Oct 05 '22 21:10

David Newswanger


The solution proposed by reka18 is great and was no doubt written specifically for the original question.

From a more general stand point, I would advise against using this kind of trick if you work on a project that has dedicated configuration files.

You are guaranteed to run into incomprehensible configuration conflicts and will possibly ignore rules that were purposefully enforced by the project.

In this case, you should use the following instead:

assuming the file is named .flake8 and is present at the project's root folder

// .vscode/settings.json
"python.linting.flake8Args": ["--config", ".flake8"],

Using --config .flake8 ensures only this file will be read (See official doc). So it is important to use this option, even though it is a default value. Otherwise, a custom user configuration in a parent folder could accidentally be used.

like image 26
Romain Vincent Avatar answered Oct 05 '22 21:10

Romain Vincent