如何在 VS Code 中使用制表符进行缩进而不收到 pylint 警告?

问题描述 投票:0回答:2

所以,我看到这个问题经常出现,但似乎提供的解决方案对我不起作用...... Pylint 似乎没有得到我想要缩进的制表符,宽度为 4 个空格。

我创建了一个简单的脚本来尝试这个:

def one_fun():
    print("hello")


if __name__ == "__main__":
    one_fun()

这是我的

.vscode/settings.json

{
    "python.analysis.typeCheckingMode": "basic",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.insertSpaces": true,
        "editor.tabSize": 4,
        "editor.detectIndentation": false
    },
}

其中还包含我在这里和那里看到的解决方案建议(只是一个例子)。

我想要 pylint 作为 linter 和 black 作为格式化程序,但似乎我遗漏了一些东西,这出现了(指针在警告上以显示自动消息)

如果我使用右上角的栏将缩进转换为空格,一切都会消失,看起来 pylint 很高兴。 这里有什么问题吗?

python visual-studio-code pylint
2个回答
0
投票

因为你想要标签,所以你的

"editor.insertSpaces": true,
应该是
"editor.insertSpaces": false,
,并且为了让pylint接受你使用标签进行缩进,你需要将以下内容放在工作区文件夹根目录下的
.pylintrc
文件中:

[FORMAT]
indent-string=\t

另见https://pylint.readthedocs.io/en/latest/user_guide/configuration/all-options.html#indent-string.

至于使用带有黑色的标签作为格式化程序,这似乎是不可能的。请参阅python black formatter 可以使用制表符而不是空格吗?.


0
投票

在您的配置中,您得到了您所期望的:宽度为 4 的制表符。只是 pylint 将制表符视为空格并发出警告。

如果你想按 Tap 插入 spaces 而不是 tabs,然后将

"editor.insertSpaces": true,
更改为
"editor.insertSpaces": false,
.

当然更方便的是点击界面右下角显示的Tab Size: *,然后进行选择

也可以在pylint中添加如下设置来消除此类错误

    // If you use the pylint extension
    "pylint.args": [
        "--disable=W0311"
    ],
    // If you use the pylint package
    "python.linting.pylintArgs": [
        "--disable=W0311"
    ]
© www.soinside.com 2019 - 2024. All rights reserved.