Skip to content

Fix WarningsRecorder.pop() returning the last match for unrelated categories - #15098

Open
Aditya-XR wants to merge 2 commits into
pytest-dev:mainfrom
Aditya-XR:fix-recwarn-pop-unrelated
Open

Aditya-XR wants to merge 2 commits into
pytest-dev:mainfrom
Aditya-XR:fix-recwarn-pop-unrelated

Conversation

@Aditya-XR

@Aditya-XR Aditya-XR commented Sep 24, 2026 •

Copy link
Copy Markdown

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, DeprecationWarning recorded, 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 exact cls match 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] pops UserWarning, because Child is a child of the later Parent match. The old greedy loop returned Parent here.

Both fail on main and pass with this change. The existing TestSubclassWarningPop tests are unchanged and still pass.

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.
Copilot AI lite review requested due to automatic review settings September 24, 2026 17:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 MateehUllah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 kilisamemarisaaa left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WarningsRecorder.pop() returns the last match instead of the first when warning categories are unrelated

4 participants