关于python:pyint用git-pylint-commit-hook忽略文件

Pylint ignore file with git-pylint-commit-hook

我使用pylint 1.6.4和git-pylint-commit-hook 2.1.1在预提交时对我的文件进行整理。我还使用alembic生成迁移,它们存储在/migrations/versions中。

问题在于所生成的迁移无效,而pylint为它们生成了警告。我可以使用--ignore = migrations忽略迁移。 (由于迁移不是python模块,而仅仅是目录,因此Pylint会默认默认忽略它们)。但是git-pylint-commit-hook会调用pylint以及已更改文件的列表进行验证。如果指定文件名列表而不是模块名,pylint不会检查是否应忽略该文件。

当有新的迁移要提交时,这会导致预提交挂钩失败。

1
2
3
4
5
6
7
8
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)

我尝试将#pylint: skip-file添加到每个迁移文件的开头。这会跳过文件,但会生成并导致文件被跳过的错误:

1
2
3
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)

所以我试图像这样忽略.pylintrc中的错误file-ignored

1
2
[messages control]
disable=unused-argument

它可以正常工作,并且不再报告错误,但是预提交挂钩失败,因为它找不到任何语句:

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

Report
======
0 statements analysed.

请注意,在这两种情况下,pylint都将文件评估为0.0 / 10.0,因此不能设置较低的失败阈值。

问题是,如何使git-pylint-commit-hook和pylint忽略我的迁移?


这是一个老问题,但我本人只是遇到了这个问题。 我在搜索此线程时找到了答案。

要解决此问题,只需添加exclude指令和与您的文件夹匹配的正则表达式即可。

例如,如果要排除迁移文件夹,只需添加:

exclude: ^tests/

到您的Pylint设置。 我的看起来像这样:

1
2
3
4
5
6
7
8
-   repo: local
    hooks:
    -   id: system
        name: PyLint
        entry: poetry run pylint
        language: system
        exclude: ^alembic/
        files: \\.py$