运行 pylint 作用于特定的 PR

Running pylint scoped to particular PR

在我看来,pylint 是一个很棒的工具,我喜欢它。但是,有些人不喜欢 pylint,我发现自己正在与不喜欢 pylint 的人合作项目。

我们的 CI 管道有多个工作流程,其中之一是 运行 的 pylint 可选“lint”工作流程。几个 PR 已合并 pylint 未通过,因此当整个 repo 运行 时 lint 工作流将永远不会通过。

我的问题:我如何才能 运行 pylint 只处理与特定拉取请求关联的文件?

那样的话,我可以让 lint 工作流的 pylint 步骤再次通过,尽管 pylint 在 运行 完整回购时失败了。

深思

我们用pre-commit到运行flake8/isort/black。我想知道是否有某种方法可以在 CI 中第二次调用 pre-commit,使用第二个配置(通过 --config 指定)以仅将范围内的文件提供给 pylint给定的公关。

请求的详细信息:我们使用 Git/GitHub。

首先说明显而易见的是,如果您想在项目中使用 pylint,您需要说服您的团队他们想要使用 pylint,这样每个人都参与其中。

其次,我认为您已经知道这一点,但您可以使用 git 技巧来做您想做的事。例如 git show --pretty="" --name-only HEAD|xargs pylint.

但这不会解决您的变更管理问题。通过使用 pylint 的功能来禁用您不想修复的消息(还没有?),可以更轻松地采用 pylint。

我建议您创建 .pylintrc 并全局禁用太长或不值得立即修复的消息。

[MESSAGES CONTROL]

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once). See also the "--disable" option for examples.
enable=
    use-symbolic-message-instead,
    useless-suppression,

# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
# option multiple times (only on the command line, not in the configuration
# file where it should appear only once).You can also use "--disable=all" to
# disable everything first and then re-enable specific checks. For example, if
# you want to run only the similarities checker, you can use "--disable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use"--disable=all --enable=classes
# --disable=W"

disable=
    attribute-defined-outside-init

如果您只有几条消息而不是全局禁用,disable them locally directly in the code or per-file

# All the violations on a single line
a, b = ... # pylint: disable=unbalanced-tuple-unpacking

All the violations on the following line
# pylint: disable-next=unbalanced-tuple-unpacking
a, b = ...

这样做,直到您确定 真正的 问题,这些问题在项目的当前状态下值得解决。然后展示pylint给团队带来的价值。完成后,您可以在 CI 中 运行 pylint,CI 只会显示 new/valuable 个问题。您可以逐步移除禁用。

a feature request to facilitate your use case in pylint by creating a baseline of the repository. This would make sure that only new messages would get reported. There's another one to help with the triaging of important issues vs nitpicky one。这将使禁用消息变得更容易,而无需考虑很多你所做的事情。但它还没有在 pylint 中(截至 2.13.0)。

为了根据需要调用 pylint,我最终利用 CI 中的 pre-commit 和辅助配置,如下所示:

pre-commit run --show-diff-on-failure --config .pre-commit-lint-config.yaml \
--from-ref origin/develop --to-ref HEAD

受到 pylint docs on pre-commit 的启发,这是我的 .pre-commit-lint-config.yaml 的一个子集:

---
default_language_version:
  python: python3.10

repos:
  - repo: local
    hooks:
      - id: pylint
        name: pylint src
        alias: pylint-src
        language: system
        files: ^src/
        types: [python]
        entry: pylint
        args:
          - "-rn"  # Only display messages
          - "-sn"  # Don't display the score
          - "--rcfile=src/pyproject.toml"