8

I am trying to set up pylint to work with pre-commit. I have looked into the docs, but still I am confused. I do not know how to setup .pre-commit-config.yaml properly.

Could you provide the most basic possible template?

-   repo: myrepo
    rev: ''  # Don't know that to type here
    hooks:
    -   id: pylint
anthony sottile
  • 61,815
  • 15
  • 148
  • 207
maslak
  • 1,115
  • 3
  • 8
  • 22

3 Answers3

18

The most basic possible template would be this:

-   repo: https://github.com/pycqa/pylint
    rev: pylint-2.6.0
    hooks:
    -   id: pylint

You can also pass arguments to pylint:

-   repo: https://github.com/pycqa/pylint
    rev: pylint-2.6.0
    hooks:
    -   id: pylint
        args:
        - --max-line-length=80
        - --ignore-imports=yes
        - -d duplicate-code

Notes about compatibility:

  • Python >=3.7 requires pylint >= 2.0
  • Python 2 requires pylint < 2.0 (Python 2 support was dropped in Pylint 2)
KevinG
  • 882
  • 3
  • 16
  • 33
  • I just went to the website u mentioned against repo and in their `README.md` they asked to use `pylint` directly as this one is deprecated. I used pylint and created a configuration file but it's giving me error, can you help? U can see the detailed error [here](https://www.reddit.com/r/learnpython/comments/fko9i7/pylint_precommit_hook_without_the_use_of_git/?utm_source=share&utm_medium=web2x) – saadi Mar 18 '20 at 12:28
  • For me, it's always getting `Skipped`, What's the possible reason. – Parvathirajan Natarajan Feb 22 '22 at 16:01
  • @ParvathirajanNatarajan by default it will run only on changed files, I think. Run `pre-commit run --all-files` to confirm you can enforce the full repo scan. – Maciej Skorski Nov 10 '22 at 09:28
5

Here is a pre-commit config fragment that works for me using regular pylint instead of mirrors-pylint:

  - repo: https://github.com/pycqa/pylint
    rev: pylint-2.5.3
    hooks:
    -   id: pylint
        args:
        - --errors-only
Todd Bradley
  • 139
  • 1
  • 7
3

According to Pylint's user guide,

Since pylint needs to import modules and dependencies to work correctly, the hook only works with a local installation of pylint (in your environment).

So you must put repo: local and install pylint locally. Their first example of .pre-commit-config.yaml looks 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
        ]
Noumenon
  • 5,099
  • 4
  • 53
  • 73