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

在pyproject.toml中为指定模块配置mypy选项失败,如何解决?

解决mypy模块级覆盖配置不生效问题

问题原因分析

  1. disable_error_code报错:mypy的模块覆盖段([[tool.mypy.overrides]])不支持单数形式的disable_error_code,正确的选项名是复数disable_error_codes。
  2. ignore_missing_imports不生效:大概率是模块匹配不精确,比如未覆盖到子模块,导致配置没有应用到目标文件。

修正后的配置

[tool.mypy]
python_version = "3.9"
disallow_untyped_defs = true
show_error_codes = true
no_implicit_optional = true
warn_return_any = true
warn_unused_ignores = true
exclude = ["scripts", "docs", "test"]


[[tool.mypy.overrides]]
module = [
    "firstmodule",
    "firstmodule.*",
    "secondmodule",
    "secondmodule.*",
    "utils",
    "utils.*",
    "config",
    "config.*",
]
disable_error_codes = ["name-defined"]
ignore_missing_imports = true

关键修正点

  • 把disable_error_code改为disable_error_codes(复数形式),符合mypy模块级覆盖的选项规范,解决配置报错问题。
  • 给每个模块添加*后缀,确保配置覆盖模块本身及其所有子模块,避免因匹配范围不足导致ignore_missing_imports不生效。
  • 确认目标模块不在全局exclude列表中,否则mypy会跳过这些模块,配置自然不会生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 12:50:24