Pylint 使用 git-pylint-commit-hook 忽略文件

Jak*_*rek 6 python pylint pre-commit pre-commit-hook alembic

我使用pylint 1.6.4和git-pylint-commit-hook 2.1.1 在预提交时整理我的文件。我还使用alembic来生成我的迁移,它们存储在<project-root>/migrations/versions.

问题是生成的迁移无效,pylint 会为它们生成警告。我可以使用 --ignore=migrations 忽略迁移。(无论如何,Pylint 都会默认忽略它们,因为迁移不是 Python 模块,它只是一个目录)。但是 git-pylint-commit-hook 调用带有更改文件列表的 pylint 进行验证。如果你给它一个文件名列表,而不是模块,pylint 不会检查文件是否应该被忽略。

当有新的迁移要提交时,这会导致 pre-commit 钩子失败。

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)
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

所以我试图忽略file-ignored.pylintrc 中的错误,如下所示:

[messages control]
disable=unused-argument
Run Code Online (Sandbox Code Playgroud)

它有效并且不再报告错误但预提交钩子失败,因为它没有找到任何语句:

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

Report
======
0 statements analysed.
Run Code Online (Sandbox Code Playgroud)

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

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

FBi*_*idu 5

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

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

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

exclude: ^tests/

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

-   repo: local
    hooks:
    -   id: system
        name: PyLint
        entry: poetry run pylint
        language: system
        exclude: ^alembic/
        files: \.py$
Run Code Online (Sandbox Code Playgroud)