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

如何在Python覆盖率报告中仅忽略指定类而不匹配相似命名类?

解决coverage仅排除LinearRegression而保留LinearRegressionMLE的问题

问题根源在于exclude_also的匹配逻辑:coverage会将该选项下的配置视为正则表达式处理,原配置里的LinearRegression会匹配所有包含该子串的内容,因此LinearRegressionMLE也被误排除。

只需把配置改为精确匹配的正则表达式,就能只命中LinearRegression类:

修改后的.coveragerc文件内容如下:

# .coveragerc to control coverage
[report]
skip_empty = true
omit = 
    __init__.py

exclude_also =
    def __repr__
    @(abc\.)?abstractmethod
    RegressionDiagnostics
    ^LinearRegression$

[html]
directory = tests/coverage_report_html

其中^代表字符串开头,$代表字符串结尾,组合后只会匹配完全名为LinearRegression的类定义,不会对LinearRegressionMLE产生影响。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 01:42:32