`WarningsRecorder.pop()` returns the last match instead of the first when warning categories are unrelated
Maintainers usually reply within 1 day
@Aditya-XR is already working on this.
Since Sep 24, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
- Domain
- testing-qa
Research direction
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.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 14.6k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 32
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pytest-dev/pytest
-
monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)Possibly taken @Maurenz2530 claimed this 8 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pytest-dev/pytest#15099 · 1 comment ·
Maintainers usually reply within 1 day
-
Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`May be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
pytest-dev/pytest#14514 · 6 comments ·
Maintainers usually reply within 1 day
-
Expose CaptureManager as public APIMay be free again A pull request for this issue was closed without being merged. Opentype: enhancement type: feature-branch
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pytest-dev/pytest#14186 · 2 comments ·
Maintainers usually reply within 1 day
-
Docs on warnings filters should note that `ResourceWarning` is often delayedPossibly taken @tanishmisra9 claimed this 103 days ago. Opentype: docs
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pytest-dev/pytest#9825 · 7 comments ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
pytest-dev/pytest#15124 · 2 comments ·
Maintainers usually reply within 1 day
All issues in pytest-dev/pytest
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
FuRongJun-1999/dsh-memory#56 ·
Maintainers usually reply within 1 day
-
Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
pgadmin-org/pgadmin4#10503 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
521xueweihan/HelloGitHub#3857 ·
-
needs-ac
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Ikalus1988/MisakaNet#2845 ·
Maintainers usually reply within 1 day
-
bug connectors operations
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pyinfra-dev/pyinfra#1989 ·
Maintainers usually reply within 3 days