skip to Main Content

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)
enter image description here

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

2

Answers


  1. 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 Can python black formatter use tabulators instead of spaces?.

    Login or Signup to reply.
  2. 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 Tap to insert spaces instead of tabs, then change "editor.insertSpaces": true, to "editor.insertSpaces": 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.

    enter image description here

    enter image description here

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

        // If you use the pylint extension
        "pylint.args": [
            "--disable=W0311"
        ],
        // If you use the pylint package
        "python.linting.pylintArgs": [
            "--disable=W0311"
        ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search