0

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?

starball
  • 20,030
  • 7
  • 43
  • 238
slim71
  • 27
  • 1
  • 9
  • Wait, it's hard to tell from the screenshot, when you press tab, is vscode actually insert a tab character or 4 spaces? – juanpa.arrivillaga May 17 '23 at 17:50
  • @juanpa.arrivillaga it's a tab. see the `->` whitespace indicator? spaces have dots as their whitespace indicators in VS Code, and tabs have `->`. What I find weird is that slim71 has `"editor.insertSpaces": true,` in their Python language settings, which should mean that the tab key inserts spaces instead of tabs. – starball May 17 '23 at 17:58
  • 4
    Actually, slim71, I'm getting mixed signals from your post. Do you want indentation with tabs or with spaces? You say "_Pylint do not seem to get that I want tabs for indent, with a width of 4 spaces_", which makes me think you want tabs, but you put `"editor.insertSpaces": true,` in your settings, and then even more confusing is the fact that you then go on to say "_If I convert indentations to spaces_", which you shouldn't need to do if you've set `"editor.insertSpaces": true,`. What's going on? – starball May 17 '23 at 17:59
  • @user I see, so the question is why isn't the configuration in the settings.json reflected in the editor? but it seems the OP *does* want tabs, so I'm confused – juanpa.arrivillaga May 17 '23 at 17:59
  • @user yes, that is my confusion as well! – juanpa.arrivillaga May 17 '23 at 18:00
  • 1
    In any case, pylint **wants you to use spaces**. Not tabs. It wants 4 spaces. I guess if you want to change that then maybe pylint is configurable in that regard – juanpa.arrivillaga May 17 '23 at 18:01
  • sorry everybody, I guess I was not clear enough! I want tabs, since I prefer them. The "width" is just to indicate that visually I'd like a tab to occupy the same as 4 spaces @user you're right: it is indeed misleading, sorry! – slim71 May 17 '23 at 19:54
  • @juanpa.arrivillaga that, I wasn't aware of! is the same valid for flake8 then? because I've tried switching and it does more or less the same (saying "indentation contains tab").... Maybe I lack this kind of knowledge, since I've was under the assumption that tabs/spaces are the same for Python, in the end... should I switch to spaces then? – slim71 May 17 '23 at 19:57
  • 1
    @slim71 yes, this is the official python Python convention, according to PEP8 – juanpa.arrivillaga May 17 '23 at 20:03
  • then I guess I wasted time trying to solve the wrong problem here... thanks! – slim71 May 17 '23 at 20:04

2 Answers2

0

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

starball
  • 20,030
  • 7
  • 43
  • 238
  • wow, such a "heated" conversation too! well then, lesson learned and migration to spaces in progress! thanks for the thorough answer, which even adds details to previous comments! – slim71 May 18 '23 at 10:42
0

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"
    ]
JialeDu
  • 6,021
  • 2
  • 5
  • 24