Pylint

What is Pylint?

Pylint is a highly versatile and widely used static code analysis tool for Python programming. Its primary function is to enhance code quality and enforce coding standards. Pylint can identify syntax errors, pinpoint areas of refactoring to improve code readability, and aligns code with PEP 8. Pylint primarily analyzes .py files. It can handle Python scripts, modules, and code written for different versions.

Installing Pylint

There are multiple methods for installing Pylint. With Trunk Check, you can automatically install and configure Pylint along with any relevant linters in a few straightforward steps. Here's how:

First, if you haven't already installed Trunk CLI, you can do so with the command below:

1curl https://get.trunk.io -fsSL | bash

Next, you can initialize Trunk from the root of your git repository:

1trunk init

This command will scan your repository and create a .trunk/trunk.yaml file that enables all linters, formatters, and security analyzers, recommended by Trunk Check. This includes Pylint if applicable to your project.

To see all available linters Trunk Check installed, simply run:

1trunk check list

If you find Pylint is not automatically enabled, you can do so by running:

1trunk check enable pylint

Alternatively, to disable flake8 run the command below. To disable other tooling applied by Trunk Check, simply replace flake8 with the respective tool you're looking to disable.

1trunk check disable flake8

For more details on Trunk Check setup, see here.

Configuring Flake8

Most linters provide some mechanism to tweak their configuration, e.g. .eslintrc or Cargo.toml. Trunk is aware of all the ways individual tools are configured and supports them. This means linters you've already configured will continue to work exactly the same, just now supercharged by Trunk Check.

If you do not have a custom config for Flake8, you'll be on our default configuration. You can check out our open-source configs repo for our always up-to-date collection of sane linter configurations.

The process for adjusting configuration and enabling plugins for Flake8 is straightforward. Flake8 has a plugin architecture where if you install a plugin, it gets used. You can enable Flake8 plugins via:

1enabled:
2 - flake8@3.9.2:
3 packages:
4 - flake8-bugbear@21.4.3

Flake8-bugbear is probably the most popular flake8 plugin and we recommend it. Here are a few other popular flake8 plugins you should consider.

  • flake8-comprehensions: Helps in identifying unnecessary comprehensions in your code.

  • flake8-docstrings: Checks for compliance with Python docstring conventions.

  • flake8-import-order: Checks the order of your imports according to various configurable ordering styles.

Here's an updated code snippet with the above Plugins enabled:

1enabled:
2 - flake8@3.9.2:
3 packages:
4 - flake8-bugbear@21.4.3
5 - flake8-docstrings@1.7.0
6 - flake8-import-order@0.18.2
7 - flake8-comprehensions@3.14.0

Since Flake8 is a Python linter, it's relevant to call out that Trunk uses hermetic runtime versions which you can override if needed. If you're using a newer version of Python than our default (3.10.8 at the time of writing) you can override it in trunk.yaml via:

1runtimes:
2 enabled:
3 - python@3.10.8

As always, you can view the defaults and configuration of everything Trunk runs via trunk config print. Note that Python runtime versions specifically are allowlisted to a limited set.

Running Flake8

To check your code with Flake8, simply run the command below. This command executes Flake8, along with any other linters Trunk Check has enabled on files you've modified.

1trunk check

If you prefer to check files you've modified with only flake8, run the following:

1trunk check --filter flake8

To check all files in your repository with Flake8, run:

1trunk check --all --filter flake8

Updating Trunk Check & Flake8

To upgrade the Trunk CLI along with all plugins and linters in your trunk.yaml simply run:

1trunk upgrade

We highly recommend running on the latest validated versions of tools as updates will frequently include important security fixes and additional valuable checks. Trunk only auto-suggests linter upgrades to versions that we have tested and support, so you may see a slight lag time when a new linter version is released.

Upgrade will also recommend new tools that have become applicable since the last time your repository was scanned. This can be a result of using new technologies in your repository or trunk itself adding support for more tools. If you don't like a particular recommendation you can always run trunk check disable <linter> to teach trunk not to recommend it.

Recommended Linters to Pair with Pylint

While Pylint is comprehensive, pairing it with other linters can enhance your code quality checks. Consider these:

  • flake8: For a quick, less comprehensive check alongside Pylint's detailed analysis.

  • Black: An opinionated code formatter that complements Pylint's style checks.

  • mypy: For type checking, especially useful in larger codebases with type annotations.

Linters Pylint Replaces

Pylint can often replace several other tools due to its extensive range of checks and features. These include:

  • pep8: Pylint covers PEP 8 styling checks and more.

  • pyflakes: For basic checks covered in Pylint's broader analysis scope.

  • pychecker: Pylint's modern capabilities outperform pychecker's offering.

Explore a Comprehensive List of Linters

For a broader view of available linters and how they integrate with various development tools, visit our extensive github repository that covers 90+ linters. 

This repository provides insights into different linters supported by Trunk, giving you a wider perspective on how to enhance your development workflow with these tools.