Search code examples
pythonvisual-studio-codepylint

How can I use tabs for indentation and not getting pylint warnings in 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) 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?


Solution

  • 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.

    You can also disable the Pylint warning as shown in another answer post here, but I think that's a bad solution, since presumably it will mean you won't get warnings if you then use indentation which is not tabs.

    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?.