Linters and formatters
How to activate and use the Python linters and formatters bundled with Pants.
Benefit of Pants: consistent interface
./pants lint
and ./pants fmt
will consistently and correctly run all your linters and formatters. No need to remember how to invoke each tool, and no need to write custom scripts.
This consistent interface even works with multiple languages, like running Python linters at the same time as Go, Shell, Java, and Scala.
Benefit of Pants: concurrency
Pants does several things to speed up running formatters and linters:
- Automatically configures tools that support concurrency (e.g. a
--jobs
option) based on your number of cores and what else is already running. - Runs everything in parallel with the
lint
goal (although not thefmt
goal, which pipes the results of one formatter to the next for correctness). - Runs in batches of 256 files by default, which gives parallelism even for tools that don't have a
--jobs
option. This also increases cache reuse.
Activating linters and formatters
Linter/formatter support is implemented in separate backends so that they are easy to opt in to individually:
Backend | Tool |
---|---|
pants.backend.python.lint.bandit | Bandit: security linter |
pants.backend.python.lint.black | Black: code formatter |
pants.backend.python.lint.docformatter | Docformatter: docstring formatter |
pants.backend.python.lint.flake8 | Flake8: style and bug linter |
pants.backend.python.lint.isort | isort: import statement formatter |
pants.backend.python.lint.pylint | Pylint: style and bug linter |
pants.backend.python.lint.yapf | Yapf: code formatter |
pants.backend.experimental.python.lint.autoflake | Autoflake: remove unused imports |
pants.backend.experimental.python.lint.pyupgrade | Pyupgrade: automatically update code to use modern Python idioms like f-strings |
To enable, add the appropriate backends in pants.toml
:
pants.toml
[GLOBAL]
...
backend_packages = [
'pants.backend.python',
'pants.backend.python.lint.black',
'pants.backend.python.lint.isort',
]
You should now be able to run ./pants lint
, and possibly ./pants fmt
:
$ ./pants lint src/py/project.py
17:54:32.51 [INFO] Completed: lint - Flake8 succeeded.
17:54:32.70 [INFO] Completed: lint - Black succeeded.
All done! ✨ 🍰 ✨
1 file would be left unchanged.
17:54:33.91 [INFO] Completed: lint - isort succeeded.
✓ Black succeeded.