Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint ignore file with git-pylint-commit-hook

Im using pylint 1.6.4 and git-pylint-commit-hook 2.1.1 to lint my files on pre-commit. I also use alembic to generate my migrations, they are stored in <project-root>/migrations/versions.

The problem is that the generated migrations are not valid and pylint generates a warning for them. I can ignore migrations with --ignore=migrations. (Pylint ignores them by default anyway because migrations isn't a python module, it's simply a directory). But git-pylint-commit-hook calls pylint with list of changed files to validate. And pylint doesn't check if the file should be ignored if you give it a list of filenames, not modules.

This causes the pre-commit hook to fail when there is a new migration to be commited.

Running pylint on migrations/versions/d1f0e08ea6d2_skill_table.py (file 2/13).. 8.6/10.00   FAILED
************* Module d1f0e08ea6d2_skill_table.py
C:  5, 0: Trailing whitespace (trailing-whitespace)
C:  1, 0: Invalid module name "d1f0e08ea6d2_skill_table" (invalid-name)
C: 16, 0: Import "from alembic import op" should be placed at the top of the module (wrong-import-position)
C: 17, 0: Import "import sqlalchemy as sa" should be placed at the top of the module (wrong-import-position)
C: 20, 0: Missing function docstring (missing-docstring)
C:171, 0: Missing function docstring (missing-docstring)

I tried to add #pylint: skip-file to beginning of each migration file. This skips the file but generates and error that the file was skipped:

Running pylint on migrations/versions/d1f0e08ea6d2_skill_table.py (file 2/13).. 0/10.00 FAILED
************* Module d1f0e08ea6d2_skill_table.py
I:  1, 0: Ignoring entire file (file-ignored)

So I tried to ignore error file-ignored in .pylintrc like this:

[messages control]
disable=unused-argument

It works and the error is no longer reported but pre-commit hook fails because it doesn't find any statements:

Running pylint on migrations/versions/d1f0e08ea6d2_skill_table.py (file 2/13).. 0/10.00 FAILED

Report
======
0 statements analysed.

Notice that in both cases pylint evaluates the files as 0.0/10.0 so setting lower threshold for failure isn't the solution.

The question is, how do I make git-pylint-commit-hook and pylint to ignore my migrations?

like image 820
Jakub Tesárek Avatar asked Aug 22 '16 08:08

Jakub Tesárek


1 Answers

This is an old question but I have just ran into this issue myself. I found the answer while searching this thread.

To solve this problem, just add an exclude directive with a regex that matches your folder.

If you want to exclude the migrations folder, by example, just add:

exclude: ^tests/

To your Pylint settings. Mine looks like this:

-   repo: local
    hooks:
    -   id: system
        name: PyLint
        entry: poetry run pylint
        language: system
        exclude: ^alembic/
        files: \.py$
like image 128
FBidu Avatar answered Sep 24 '22 03:09

FBidu