Let's meet Pylint: Python Linter
Tags: python, tools, pylint
Published: 2024-02-12 11:00:00

After the post about [Black](https://dev.to/cereal84/lets-meet-black-python-code-formatting-2n6n?preview=8e6d3832c4ee962658e87545120d063e0dd9b7209b85efca44cfaa2c9f3dca71609a009ec342b33763b42628cd5610affaec2ac9ad5e2647490a5d18) (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 undeﬁned variables
- unused import
- enforce consistent coding practice
- etc.

You can find the full list of checks [here](https://pylint.readthedocs.io/en/latest/user_guide/checkers/features.html).

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=<file[,file]>
Ignore one or more files.

for a full list of options please use _--help_ option or check the [documentation](https://docs.pylint.org/run.html#command-line-options).

## 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](https://docs.pylint.org/plugins.html).

## 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](https://github.com/ikamensh/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.