Conversation
The "best inexact match" loop replaced the current best whenever the new category was not a subclass of it, which also holds for unrelated sibling categories, so pop() returned the last recorded warning instead of the first. Return the first match whose category is not a strict subclass of another match's category, as the docstring describes. Fixes pytest-dev#15097.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No unresolved blocking issues were identified.
Review effort: Lite
Findings: None
What changed in this PR
Fixes WarningsRecorder.pop() to return the first valid warning when matching categories are unrelated or ordered through inheritance.
Changes:
- Reworked warning matching to exclude only strict subclasses.
- Added regression tests.
- Added changelog entry and contributor attribution.
| File | Description |
|---|---|
testing/test_recwarn.py |
Adds regression coverage. |
src/_pytest/recwarn.py |
Corrects warning selection logic. |
changelog/15097.bugfix.rst |
Documents the fix. |
AUTHORS |
Adds contributor attribution. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
MateehUllah
left a comment
There was a problem hiding this comment.
Reviewed the updated warning-selection algorithm. Building the complete set of matching categories before selecting the first category that is not a strict subclass of another match correctly avoids the previous order-dependent behavior. Identical categories are appropriately ignored in the strict-subclass comparison, unrelated categories preserve recording order, and the added tests cover both sibling categories and a child preceding a later parent. I did not find a blocking correctness issue.
kilisamemarisaaa
left a comment
There was a problem hiding this comment.
Reproduced and reviewed at head f33818b206a29a490baacac16dc135e2ecb243b2 on Windows/Python 3.12.6.
The selection logic now preserves recording order for unrelated warning categories while skipping a category that is a strict subclass of a later match; the identity guard correctly keeps duplicate categories from excluding one another. The two new regressions cover both cases.
Validation: SETUPTOOLS_SCM_PRETEND_VERSION=9.2.0.dev999 uv run --reinstall pytest testing/test_recwarn.py -q -> 69 passed. I found no blocking issue.
Fixes #15097.
WarningsRecorder.pop()picked its "best inexact match" by walking the list and replacing the current best whenever the new warning's category was not a subclass of the best one. That condition also holds for unrelated sibling categories, so with e.g.UserWarning, RuntimeWarning, DeprecationWarningrecorded,pop()returned the last one instead of the first.The new implementation follows the docstring literally: collect all warnings matching
cls, then return the first one whose category is not a strict subclass of another match's category. An exactclsmatch can never be a strict subclass of another match, so the old exact-match fast path is covered by the same rule.Tests:
test_pop_finds_first_of_unrelated_matches: sibling categories pop in recording order.test_pop_skips_child_of_later_match:[Child, UserWarning, Parent]popsUserWarning, becauseChildis a child of the laterParentmatch. The old greedy loop returnedParenthere.Both fail on main and pass with this change. The existing
TestSubclassWarningPoptests are unchanged and still pass.