Skip to content

vim flake8/pylint: ‘bad-indentation (found 4 tabs, expected 1)’

check your whitespace

Update: For reasons that I forget, I had indent-string=\t in the [FORMAT] section of my user ~/.pylintrc. Since commenting it out seems to have had no averse effects and since I’ve forgotten the reason I added it (assuming I did deliberately add it and it wasn’t generated) I have now removed that directive.

A while back I beefed up vim with some plugins, mostly around python programming and one for ledger, or my case the excellent hledger entry. This is all great until something starts conflicting or stops working.

For some reason — system update, plugin update, phase of moon changed — I started seeing lint complaints on python scripts I had previously written:

[bad indentation] found 4 tabs, expected 1 (from within vim)

W0311: Bad indentation. Found 4 tabs, expected 1 (bad-indentation) (from running pylint directly)

or similar, “Found 8 tabs, expected 2”, “Found 12 tabs, expected 3”, and so on.

I think the issue was due to a combination of flake8 (vim plugin) / pylint (linter) and a virtualenv (virtual environment) I had set up for particular projects. For reason of remembering in the future, the following sorted it:

  • activate virtual environment (eg source .env/bin/activate or similar)
  • install pylint in that venv (eg pip3 install pylint)
  • set up a pylintrc for the venv (eg pylint --generate-rcfile > .pylintrc)

That seemed to sort out the W0311 issues.

As a post-script, I had a look at the file itself to see if there were indeed a ridiculous number of tabs, which pylint should rightly complain about. Using eg xxd to view a hexdump of a file (always a handy tool to have), a line might look like:

00000ed0: 2020 2023 2069 6620 636c 6970 6e75 6d20 # if clipnum

the first part, 00000ed0, is the location in the file. The second part, 2020 2023 2069 6620 636c 6970 6e75 6d20 is the hex representation of the file contents, and the last part is the plain text. Interestingly, the 20s (or 0x20) are decimal 32s, and 32 is the character code for space. I cross-checked with a new file created in vim with only a tab character, and…

00000000: 090a ..

Here the character is 09 (or 0x09), which is indeed a tab character! It would seem that a plugin is setting my tabbstop, shiftwidth and expandtab but only for for python files.

1 thought on “vim flake8/pylint: ‘bad-indentation (found 4 tabs, expected 1)’”

  1. Pingback: Quick Scripts: Summary of Video Directory – Rob's Blog

Tell us what's on your mind

Discover more from Rob's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading