`WarningsRecorder.pop()` returns the last match instead of the first when warning categories are unrelated
维护者通常 1 天内回复
@Aditya-XR 已经在做这个了。
开始于 2026年9月24日。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
- 领域
- testing-qa
调研方向
Start at the WarningsRecorder.pop implementation and read TestSubclassWarningPop plus test_recording, especially the existing category-selection loop. Add a regression case using unrelated warning categories and verify that pop returns the first matching warning; run the relevant warning-recorder tests.
由索引模型根据 Issue 内容生成。
描述
WarningsRecorder.pop() (and pytest.warns(...).pop() / recwarn.pop()) returns the last matching warning instead of the first one when the recorded warnings have categories that are unrelated to each other.
import warnings
def test_pop(recwarn):
warnings.warn("first", UserWarning)
warnings.warn("second", RuntimeWarning)
warnings.warn("third", DeprecationWarning)
assert str(recwarn.pop().message) == "first"
E AssertionError: assert 'third' == 'first'
The docstring says pop returns "the first recorded warning which is an instance of cls, but not an instance of a child class of any other match". None of the three categories is a subclass of another, so the first one should come back. That was also the behaviour before 8.0.
The cause is the "best inexact match" loop added in #11160 (the fix for #10701):
if issubclass(w.category, cls) and (
best_idx is None
or not issubclass(w.category, self._list[best_idx].category)
):
best_idx = i
not issubclass(new, best) is true both when new is more general than best and when the two are unrelated siblings. So every sibling replaces the current best, and the loop ends up on the last one.
The existing tests in TestSubclassWarningPop only use a single parent/child chain, so they don't catch this. test_recording pops a UserWarning and a DeprecationWarning, but both have the message "hello", so it passes whichever one comes back.
Reproduced on main (pytest 9.2.0.dev345+g872117358), Python 3.13.5, Windows 11. The loop was introduced by #11160, so the bug is present since 8.0.0; in 7.4.x pop() returned the first match. I have a fix with regression tests ready and will open a PR.
- 主要语言
- Python
- 星标
- 14.6k
- 派生
- 3.4k
- 平均合并
- 2 天 7 小时
- 30 天内合并 PR
- 29
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytest-dev/pytest 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
pytest-dev/pytest#15146 ·
维护者通常 1 天内回复
-
monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)可能已有人在做 @Maurenz2530 于 10 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
pytest-dev/pytest#15099 · 3 条评论 ·
维护者通常 1 天内回复
-
Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
pytest-dev/pytest#14514 · 6 条评论 ·
维护者通常 1 天内回复
-
Expose CaptureManager as public API可能重新可做 关联的 PR 已关闭且未合并。 未关闭type: enhancement type: feature-branch
难度 2/5 1-3 小时 新手友好度 68/100
pytest-dev/pytest#14186 · 2 条评论 ·
维护者通常 1 天内回复
-
Docs on warnings filters should note that `ResourceWarning` is often delayed可能已有人在做 @tanishmisra9 于 106 天前认领。 未关闭type: docs
难度 2/5 1-3 小时 新手友好度 68/100
pytest-dev/pytest#9825 · 7 条评论 ·
维护者通常 1 天内回复
查看 pytest-dev/pytest 的全部 Issue
相似的 Issue
-
area/i18n comp/desktop P3 type/feature
难度 2/5 1-3 小时 新手友好度 65/100
NousResearch/hermes-agent#134377 ·
维护者通常 1 天内回复
-
good first issue needs-triage priority: medium
难度 2/5 1-3 小时 新手友好度 78/100
melodic-software/claude-code-plugins#6499 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
ssciwr/clang-format-wheel#167 ·
-
conda skeleton cran has SSL_NO_VERIFY check inverted and TLS verification off by default可能已有人在做 @matthewfeickert 今天认领。 未关闭type::bug
难度 2/5 1-3 小时 新手友好度 82/100
conda/conda-build#6202 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复