使用Pylint按目录忽略

68 投票
9 回答
83140 浏览
提问于 2025-04-15 20:46

以下内容来自于 Pylint 的文档

--ignore=<file>
    Add <file or directory> to the black list. It should be a 
    base name, not a path. You may set this option multiple 
    times. [current: %default]

不过,我在让目录部分正常工作上运气不太好。

我有一个叫 migrations 的文件夹,里面有 django-south 的迁移文件。当我输入 --ignore=migrations 时,它还是不断给我 migrations 文件夹里的文件报错和警告。

难道 --ignore 对文件夹不管用吗?

如果我能用正则表达式来匹配要忽略的文件,那就好了,因为 django-south 的文件都是以 0001_something、0002_something 开头命名的……


由于我无法让忽略目录的功能正常工作,我只好在每个迁移文件的顶部加上 # pylint: disable-msg-cat=WCREFI,这样就可以忽略所有 Pylint 的错误、警告和信息了。

9 个回答

16

为了忽略名为 3rdparty 的目录树下的子目录,我们在 .pylintrc 文件中的 [MASTER] 部分添加了以下 ignore-patterns 条目。

# Add files or directories matching the regex patterns to the blacklist. The
# regex matches against base names, not paths.
# Ignore all .py files under the 3rdparty subdirectory.
ignore-patterns=**/3rdparty/**/*.py

这样做解决了我们在使用 Pylint 1.7.1 时遇到的问题。

最开始我们对评论中的“基本名称”这一条款感到困惑。显然,它确实接受带有通配符的路径。至少对我们来说是这样。

17

在Fabien的回答基础上,--ignore-paths的用法是这样的。

ignore-paths=^src/experiments/.*$,
             ^src/ingredients/.*$,
             ^src/scripts/.*$,
             ^src/tests/.*$

如果你想在你的pyproject.toml文件中进行配置,配置的样子大概是这样的:

[tool.pylint.MASTER]
ignore-paths = '^src/experiments/.*$'

https://github.com/PyCQA/pylint/issues/2686#issuecomment-864585778

52

在我的 .pylintrc 文件中添加以下内容可以让 Pylint 0.25 正常工作:

[MASTER]
ignore=migrations

我遇到的问题是 PyDev 似乎没有遵循我的设置。我觉得这是因为它是按文件来运行 Pylint 的,这样可能会绕过“忽略”检查,无论是针对模块/目录还是文件。从 PyDev 调用 Pylint 的方式看起来是这样的:

/path/to/site-packages/pylint/lint.py --include-ids=y /path/to/project/migrations/0018_migration.py

撰写回答