How can I use tabs for indentation and not getting pylint warnings in VS Code?

huangapple go评论80阅读模式
英文:

How can I use tabs for indentation and not getting pylint warnings in VS Code?

问题

因为你想要使用制表符,而不是空格,所以你需要在VS Code的设置中将editor.insertSpaces设置为false,这样VS Code才会使用制表符进行缩进。此外,确保你的文件中实际使用的是制表符而不是空格。

英文:

So, I've seen that this issue comes up a lot, but it seems the solutions provided are not working for me... Pylint do not seem to get that I want tabs for indent, with a width of 4 spaces.

I've created a simple script to try this out:

def one_fun():
	print("hello")


if __name__ == "__main__":
	one_fun()

Here's my .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
    },
}

which also contains what I've seen proposing as solution here and there (just an example).

I wanted to have pylint as linter and black as formatter, but it seems I'm missing something and this comes up (pointer is on the warning to show the automatic message)
How can I use tabs for indentation and not getting pylint warnings in VS Code?

If I convert indentations to spaces, using the top right bar, everything disappears and pylint is happy, it seems.
Is there something wrong here?

答案1

得分: 0

你遇到的错误是"bad-indentation (W0311)"。由于您_希望_使用制表符,您的"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

至于在使用 Black 作为格式化程序时使用制表符,似乎不可行。请参阅 https://stackoverflow.com/q/59135128/11107541

英文:

The error you're getting is "bad-indentation (W0311)". Since you want tabs, your "editor.insertSpaces": true, should instead be "editor.insertSpaces": false,, and to make pylint accept your usage of tabs for indentation, you need to put the following in a .pylintrc file in the root of your workspace folder:

[FORMAT]
indent-string=\t

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

As for using tabs with Black as your formatter, it seems that that's not possible. See https://stackoverflow.com/q/59135128/11107541.

答案2

得分: 0

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

如果你想要按<kbd>Tap</kbd>来插入空格而不是制表符,那么将 &quot;editor.insertSpaces&quot;: true, 更改为 &quot;editor.insertSpaces&quot;: false,

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

你也可以添加以下设置以消除pylint中的此类错误。

    // 如果你使用pylint扩展
    &quot;pylint.args&quot;: [
        &quot;--disable=W0311&quot;
    ],
    // 如果你使用pylint包
    &quot;python.linting.pylintArgs&quot;: [
        &quot;--disable=W0311&quot;
    ]
英文:

In your configuration you've got what you expect: tabs with a width of 4. It's just that pylint treats a tab as a space and raises a warning.

If you want to press <kbd>Tap</kbd> to insert spaces instead of tabs, then change &quot;editor.insertSpaces&quot;: true, to &quot;editor.insertSpaces&quot;: false,.

Of course, it is more convenient to click Tab Size: * displayed in the lower right corner of the interface, and then make a selection.

How can I use tabs for indentation and not getting pylint warnings in VS Code?

How can I use tabs for indentation and not getting pylint warnings in VS Code?

You can also add the following settings to eliminate such errors in pylint.

    // If you use the pylint extension
    &quot;pylint.args&quot;: [
        &quot;--disable=W0311&quot;
    ],
    // If you use the pylint package
    &quot;python.linting.pylintArgs&quot;: [
        &quot;--disable=W0311&quot;
    ]

huangapple
  • 本文由 发表于 2023年5月18日 01:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定