You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django项目运行Pylint预提交钩子时出现'fatal未定义'错误

pre-commit集成Pylint时自定义评分公式报错:name 'fatal' is not defined

问题详情

我在pre-commit与Pylint集成过程中使用了以下自定义评分公式:

evaluation=max(0, 0 if fatal else  10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10))

执行命令 pre-commit run --all-files 时,抛出异常:

An exception occurred while rating: name 'fatal' is not defined

我的pre-commit配置文件内容如下:

repos:
  - repo: https://github.com/pycqa/pylint
    rev: v2.12.2
    hooks:
      - id: pylint
        args:
          - --fail-under=9
          - --rcfile=./.pylintrc
          - --disable=E0401 # for disabling import errors
        exclude: '/migrations/'

问题原因

你使用的Pylint v2.12.2版本不支持在自定义评分公式中使用fatal变量——该变量是在Pylint v2.13版本后才新增的,用于单独统计致命级错误。在v2.12.2中,致命错误被归类到error变量的统计范围内,没有单独暴露fatal变量,因此公式会触发未定义异常。

解决方法

方案1:升级Pylint版本

将pre-commit配置中的Pylint版本升级到v2.13或更高稳定版本,即可正常使用fatal变量:

repos:
  - repo: https://github.com/pycqa/pylint
    rev: v2.13.0 # 或更高版本,如v2.17.7
    hooks:
      - id: pylint
        args:
          - --fail-under=9
          - --rcfile=./.pylintrc
          - --disable=E0401
        exclude: '/migrations/'

升级后无需修改原评分公式,fatal变量会正确统计致命错误。

方案2:调整评分公式适配旧版本

如果无法升级Pylint,可以修改.pylintrc中的评分公式,利用error变量替代fatal的判断逻辑(v2.12.2中致命错误包含在error计数里):

# 调整逻辑:存在错误(含致命错误)则评0分,否则计算扣分
evaluation=max(0, 0 if error > 0 else  10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10))

若需要仅针对致命错误判0分,普通错误仍参与扣分,可通过禁用非致命的error规则,让error变量仅统计致命错误,但这种方式灵活性较低。

内容的提问来源于stack exchange,提问作者deepu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:00:26