After the post about Black (a formatter for Python) today we'll talk about another tool, Pylint (plus one you can find at the end before the "Conclusion" section).
Pylint is a linter... ok, a step back, what is a linter?
A linter is a static code analyzer used to find programming errors, bugs, stylistic errors, and suspicious constructs. This kind of tool can:
- find syntax errors
- find unused variables and undefined variables
- unused import
- enforce consistent coding practice
- etc.
You can find the full list of checks here.
So helps to find some problems earlier contributing to higher-quality code.
It is better to specify, that the real improvement in quality code happens only by the developer writes a code more readable, uses the right design patterns, etc.
Usually, the linter builds a report about the code quality according to the guidelines used to configure it.
Due Python is an interpreted language a linter is useful for finding some errors before running the code.
Basic
As usual, the first step is to install it, you can do it in your system or in a virtual environment.
pip install pylint
The easiest way to run it is to run it file by file or in a directory
$ pylint {source_file_or_directory}
The code will be rated:
\-----------------------------------
Your code has been rated at 4.10/10
Options
By default, all the checks are enabled.
--disable=all
Disable all the checks.
--disable=id
Disable a specific check.
You can specify more checks by using commas
--disable=<id_1>,<id_2>,...,<id_N>
--enable=id
Works exactly like the disable option.
--ignore=
Ignore one or more files.
for a full list of options please use --help option or check the documentation.
Configuration
Pylint supports a configuration file with all the configuration you need. To use it you need to place a file called .pylintrc or pylintrc into your directory.
Well, Pylint searches for this kind of file by looking in the following directory order and uses the first one it found:
- working directory
- The file named by environment variable PYLINTRC
- .pylintrc in your home directory
Plugin
It is possible to extend the behavior of Pylint using or writing plugins.
This topic is out of the scope of this post but you can find all the information and examples in the documentation.
My 2 Cents
Someone uses this rate as a quality gate, this means that if the code does not reach a threshold then is not possible to push the code into a branch, create PR, etc.
In my opinion, if you want to use that as a quality gate do not use it as a threshold for a full score, or better do not use it as a quality gate :D. Remember that a full score does not mean that the code has high quality.
It can be a useful tool if used together with pre-commit because it can prevent you from sending code with some errors/problems by easing the code review. And it can be very useful to the newly hired especially if they are junior position.
Pylint likes the f-string instead of the use of "".format()
so I suggest to use flynt before pylint. This tool replaces "%-formatted" and .format(...) strings into f-strings.
Conclusion
Like every tool it can be helpful if used correctly or slow down the work if used wrongly.
In any case, I recommend anyone to use this type of tool at least once; but don't do it on projects with a large codebase because it could be frustrating, in that case run it on one or a few files at a time.
Top comments (0)