Search code examples
pythonindentationpylintstatic-code-analysislinter

`pylint`: how do you enable errors for tabs or spaces indentation?


I am trying to enforce some simple python formatting rules. I found pylint and I have been very happy. However one of the more simple formatting checks I need to enforce is: tabs-only or spaces-only indentation.

In pylint, how do you enable errors or warnings for tabs or spaces indentation?

I see that pylint has w0311 "Used when an unexpected number of indentation's tabulations or spaces has been found". But w0311 does not enforce tabs-only or spaces-only... it still supports tabs or spaces.

I need all my python files to be only one type of indentation.

(

p.s. If you are curious how I use pylint to enforce my rules. I have a shell script that runs pylint and I use set -o errexit and this is hooked in with the build. So if pylint finds something it exits with nonzero value and causes the build to fail.

)


Solution

  • Well what you are looking for might be this:

    mixed-indentation (W0312): Found indentation with %ss instead of %ss Used when there are some mixed tabs and spaces in a module.

    Which is raised when:

    Description

    Used when there are some mixed tabs and spaces in a module.

    Explanation

    Python interprets tabs and spaces differently, so consistent indentation is critical to the correct interpretation of blocks in Python syntax.

    This warning is raised when a mix of both spaces and tabs is used in indentation—or more precisely, when an indent is detected that is not consistent with the indent-string option. By default, indent-string is set to four spaces, the style of indentation recommended in PEP 8.

    Edit #1:

    Pylint does not have an option that I know of (and as of the moment) to "enforce" only tabs/spaces in your coding style. The above mentioned W0312 exists to warn for indentation inconsistencies on your code.

    There is a trick that I use to enforce indentation: Open your project on PyCharm (works on the community edition also which is free) if the IDE detects inconsistencies it will warn you about them and it will give you an option to change indentation of the current file, or keep it as it is!