Pre-commit integrationΒΆ
pylint can be used as a pre-commit hook. We however
discourage it as pylint -- due to its speed -- is more suited to a continuous integration
job or a git pre-push hook, especially if your repository is large.
Since pylint needs to import modules and dependencies to work correctly, the
hook only works with a local installation of pylint (in your environment). It means
it can't be used with pre-commit.ci, and you will need to add the following to your
.pre-commit-config.yaml
.. sourcecode:: yaml
- ci:
skip: [pylint]
Another limitation is that pylint should analyse all your code at once in order to best infer the
actual values that result from calls. If only some of the files are given, pylint might
miss a particular value's type and produce inferior inference for the subset. Since pre-commit slices
the files given to it in order to parallelize the processing, the result can be degraded.
It can also be unexpectedly different when the file set changes because the new slicing can change
the inference. Thus the require_serial option should be set to true if correctness and determinism
are more important than parallelization to you.
If you installed pylint locally it can be added to .pre-commit-config.yaml
as follows. As pylint is slow, a popular option is to run it only before pushing with a
git pre-push hook instead of on every commit: you can do that with the stages
option, as documented in pre-commit's guide on confining hooks to run at certain
stages:
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
stages: [push]
require_serial: true
args:
[
"-rn", # Only display messages
"-sn", # Don't display the score
]
If you want to keep running pylint on every commit, simply omit the stages
option. You can use args to pass command line arguments as described in the
Tutorial. A hook with more arguments could look something like this:
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
args:
[
"-rn", # Only display messages
"-sn", # Don't display the score
"--rcfile=pylintrc", # Link to your config file
"--load-plugins=pylint.extensions.docparams", # Load an extension
]