在pyproject.toml中为指定模块配置mypy选项失败,如何解决?
解决mypy模块级覆盖配置不生效问题
问题原因分析
disable_error_code报错:mypy的模块覆盖段([[tool.mypy.overrides]])不支持单数形式的disable_error_code,正确的选项名是复数disable_error_codes。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
相关产品推荐
相关产品推荐

