python - git-pylint-commit-hook 不检查所有项目文件

标签 python python-2.7 pylint

我有一个 python 2.7 项目,它有一个主文件 main.py 和一个 src 文件夹,其中包含 main.py 中使用的其他 python 文件:

project folder
|
+ main.py
|
+-src(folder)
   +-file1.py
   |
   +....

现在,我想使用 git-pylint-commit-hook 在我的所有 python 文件上运行 lint。我使用 pip install git-pylint-commit-hook 安装了该软件包。我添加了一个包含

的文件
#!/usr/bin/env bash
git-pylint-commit-hook

在/project/root/.git/hooks/pre-commit 文件夹中并使其可运行。

问题是当我提交更改时,git-pylint-commit-hook 只检查 main.py 而不会检查 src 文件夹中的所有文件。

我该如何解决这个问题?

如果我直接使用参数 *.py **/*.py 运行 pylint ,它会检查所有文件。

这是我尝试过的,我设置了pylintrc文件来设置参数。我在项目文件夹中添加了一个 pylintrc 文件

[pre-commit-hook]
params=*.py **/*.py

但是它不起作用。

如何解决这个问题?

最佳答案

您的设置在我的环境中可以正常工作,不需要任何 .pylintrc 文件或指定任何特殊的 pylint 配置。我认为该钩子(Hook)仅检查 main.py ,因为 git add 中仅包含 main.py。确保您已暂存整个 src 文件夹,并且 git 不会忽略它,以便将其包含在 git commit 中。

$ tree -a -L 2
.
├── .git
│   ├── HEAD
│   ├── ...
│   └── refs
├── main.py
└── src
    ├── file1.py
    └── file2.py

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   main.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    src/

这里,git status 报告仅 main.py 已暂存,src (及其下的所有内容)尚未跟踪,这意味着在执行 git commit 时不会包含它:

$ git commit -m "test"
Running pylint on main.py (file 1/1)..  3.3/10.00   FAILED
************* Module main
C:  1, 0: Missing module docstring (missing-docstring)
...

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

正如预期的那样,只有 main.py 被提交,并且只有 main.pygit-pylint-commit-hook 检查。一旦我 git add src 文件夹,它将包含在下一次尝试提交中。

$ git add src
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   main.py
    new file:   src/file1.py
    new file:   src/file2.py

$ git commit -m "test"
Running pylint on main.py (file 1/3)..  3.3/10.00   FAILED
...


Running pylint on src/file1.py (file 2/3).. 3.3/10.00   FAILED
...


Running pylint on src/file2.py (file 3/3).. 3.3/10.00   FAILED
...

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

关于python - git-pylint-commit-hook 不检查所有项目文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57130385/

相关文章:

python - python中的数据框操作

python - 仅当类别的所有项目都为 True 时才返回行

python - 将列表 A 的每个元素与列表 B 的每个元素压缩 - 最佳方法 "pythonie"

python - 在我的 Raspberry PI 上使用 Python 和 OpenCV 没有保存视频文件

python - 在 matplotlib 中格式化 3d 条形图

python - 使用 pika 在 python 中使用 SparkStreaming、RabbitMQ 和 MQTT

python - 如果 python 包依赖于我使用的 SQL 数据库,如何将其公开?

python - .Pytest session 范围固定装置显示 pylint 中的错误

python - Pylint 配置问题

Notepad++ 的 Python 正确性(即 lint)分析