使用 Pylint 按目录忽略

问题描述 投票:0回答:9

以下内容来自 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 错误、警告和信息。

python pylint
9个回答
47
投票

将以下内容添加到我的 .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

15
投票

为了忽略名为

3rdparty
的目录树下的子目录,我们将以下
ignore-patterns
条目添加到
[MASTER]
中的
.pylintrc
条目中。

# 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 的问题。

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


12
投票

您不能给出路径,而只能给出目录的“基本名称”。例如,使用

--ignore=lib
代替
--ignore-=appengine-toolkit/gaetk/lib

问题是您将忽略名为 lib

all
目录。


12
投票

截至目前,

--ignore
不适用于目录(GitHub 上有一个未解决的问题,忽略子句不忽略目录#2686)。

事实证明,只有文件名,而不是整个路径针对黑名单进行了测试。 (正如同一个拉取请求中所指出的:geajack 的评论

选项

--ignore-patterns
也有同样的问题,但是当我检查时尝试修复它(添加
ignore-paths
以匹配完整路径#3266

对于您的情况,现在最好的解决方案是使用正则表达式来匹配您在文件中想要的模式,这也是我的情况。由于我不太精通正则表达式,所以我使用了Regex101,这是我推荐的。


11
投票

添加到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


4
投票

实际上,Pylint 2.3.1 存在一个未解决的问题。

如果你在忽略选项中设置了一个目录,它就不会忽略它。


3
投票

从 Pylint 2.9 开始,将有一个新的配置目录

ignore-paths
,它与整个路径而不是基本名称匹配正则表达式。

另请参阅:


1
投票

看来你需要做一些一些猴子补丁,这对我来说适用于 Pylint 版本 2.5.3。我只是不知道为什么 Pylint 没有修复忽略路径的问题。

将其添加到您的 .pylintrc 文件中:

init-hook=
    sys.path.append(os.getcwd());
    from pylint_ignore import PylintIgnorePaths;
    PylintIgnorePaths('my/thirdparty/subdir', 'my/other/badcode')

然后创建文件pylint_ignore.py:

from pylint.utils import utils


class PylintIgnorePaths:
    def __init__(self, *paths):
        self.paths = paths
        self.original_expand_modules = utils.expand_modules
        utils.expand_modules = self.patched_expand

    def patched_expand(self, *args, **kwargs):
        result, errors = self.original_expand_modules(*args, **kwargs)

        def keep_item(item):
            if any(1 for path in self.paths if item['path'].startswith(path)):
                return False

            return True

        result = list(filter(keep_item, result))

        return result, errors

-2
投票

然后您可以使用 Bash 扩展来发挥您的优势:

--ignore=migrations/{0000..1000}_something
© www.soinside.com 2019 - 2024. All rights reserved.