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

mypy exclude配置项未生效,是否为bug?

pytest-mypy 不遵守 mypy.ini 排除规则的问题

复现步骤

  • 创建文件夹MyProjectFolder
  • 创建空文件helloroot.py
  • 新建子文件夹FolderNotIgnored
  • 在该子文件夹里创建hello.py
  • 编写mypy.ini文件:
[mypy]
exclude=FolderNotIgnored/

目录结构如下:

│   helloroot.py
│   mypy.ini
└───FolderNotIgnored
        hello.py

问题现象

运行 pytest --mypy 后,明明配置了排除FolderNotIgnored,但该文件夹下的hello.py还是被纳入了检测:

================================================================= test session starts ==================================================================
platform win32 -- Python 3.12.5, pytest-8.3.4, pluggy-1.5.0
rootdir: C:\Users\CENSORED\Documents\Challenges\MyProjectFolder
plugins: anyio-4.8.0, hydra-core-1.3.2, mypy-0.10.3
collected 3 items                                                                                                                                         

FolderNotIgnored\hello.py ..                                                                                                                      [ 66%]
helloroot.py .                                                                                                                                    [100%] 
========================================================================= mypy ========================================================================= 

Success: no issues found in 2 source files
================================================================== 3 passed in 0.10s =================================================================== 

解决办法

pytest-mypy 插件默认不会自动读取 mypy.ini 里的exclude规则,得单独配置:

方法1:用 pytest 配置文件指定排除

在项目根目录创建pytest.ini,添加:

[pytest]
mypy_exclude = FolderNotIgnored/

如果用pyproject.toml,则添加:

[tool.pytest.ini_options]
mypy_exclude = "FolderNotIgnored/"

方法2:命令行直接指定排除规则

运行命令时加上--mypy-exclude参数:

pytest --mypy --mypy-exclude FolderNotIgnored/

额外提示

如果你的 pytest-mypy 版本比较老(比如示例里的0.10.3),可能存在配置兼容问题,升级到最新版本试试:

pip install --upgrade pytest-mypy

内容的提问来源于stack exchange,提问作者FluidMechanics Potential Flows

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 05:12:44