VS Code 的 flake8 的忽略参数在保存时对格式不起作用?

VS Code's flake8's ignore arguments don't work on format on save?

这是我的 settings.json:

{
    "python.pythonPath": "/home/zhaodachuan/anaconda3/envs/ranking_engine/bin/python",
    "python.autoComplete.extraPaths": [
        "/mnt/c/Users/hnjyz/OneDrive/jupyter_lab/code/ranking_engine",
    ],
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120",
        "--ignore=E402,F401",
    ],
}

但是当我在 VS Code 中打开 format on saveflake8 --ignore 不起作用。

我该怎么办?

Flake8 不是格式化程序,它是 linter

肯定是one of the supported linters of VS Code's Python extension,但不会影响设置editor.formatOnSave,也不会被格式化文档命令触发。如果配置正确,它应该会自动 运行 并检查您的文件是否存在可能的问题,然后将它们显示在“问题”选项卡上:

如果我添加:

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

那么之前显示的 F401 错误应该会消失:

您似乎要寻找的是 格式化程序。请参阅 Python 上 VS Code 文档的 Formatting 部分:https://code.visualstudio.com/docs/python/editing#_formatting:

Formatting makes code easier to read by human beings by applying specific rules and conventions for line spacing, indents, spacing around operators, and so on (see an example on the autopep8 page). Formatting doesn't affect the functionality of the code itself. (Linting, on the other hand, analyzes code for common syntactical, stylistic, and functional errors as well as unconventional programming practices that can lead to errors. Although there is a little overlap between formatting and linting, the two capabilities are complementary.)

The Python extension supports source code formatting using either autopep8 (the default), black, or yapf.

安装选定的格式化程序之一,并启用 formatOnSave:

    "[python]": {
        "editor.formatOnSave": true
    },
    "python.formatting.provider": "yapf",
    "python.formatting.yapfPath": "/usr/local/bin/yapf",
    "python.formatting.yapfArgs": [
        "--style=/path/to/setup.cfg"
    ],