Skip to content

Make WarningsRecorder.pop return the first least-derived match - #15541

Open
Jania02 wants to merge 4 commits into
pytest-dev:mainfrom
Jania02:fix-warnings-recorder-pop-order
Open

Jania02 wants to merge 4 commits into
pytest-dev:mainfrom
Jania02:fix-warnings-recorder-pop-order

Conversation

@Jania02

@Jania02 Jania02 commented Dec 5, 2026

Copy link
Copy Markdown

WarningsRecorder.pop(cls) returned the last match instead of the first one when the recorded warnings had unrelated categories (siblings that are both subclasses of cls). The loop replaced the best candidate every time the new category was not a subclass of the current one, so with siblings the last one always won.

The fix collects the matching categories, keeps only the ones that are not a subclass of another match, and returns the first recorded warning with one of those categories. This follows the docstring ("Pop the first recorded warning which is an instance of cls, but not an instance of a child class of any other match").

I first tried comparing every match against every other match, but it was O(n²): pop took ~700 ms with 5000 recorded warnings. The current version only compares distinct categories, so it stays linear (on my machine, 0.97 ms before and 0.81 ms after with 5000 warnings).

Testing:

  • Added two regression tests in testing/test_recwarn.py; both fail on main and pass with the fix.
  • Full test suite on Windows, Python 3.14: 4692 passed before, 4694 passed after, no failures.

Closes #15097

  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • Add text like closes #XYZW to the PR description.
  • If AI agents were used, they are credited in commit trailers.
  • Create a new changelog file in the changelog directory.
  • Add yourself to AUTHORS in alphabetical order.

AI disclosure: I used Claude (Anthropic) as an assistant to reproduce the bug, draft the tests and the fix, and measure performance. The commits are credited with an Assisted-by trailer, as allowed by the contributing guide. I reviewed and understand the change and I will answer the review myself.

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Dec 5, 2026
Assisted-by: Claude (Anthropic, claude-opus-5-5)

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

bot:chronographer:provided (automation) changelog entry is part of PR

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

1 participant