Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ James Bourbeau
James Frost
James V
Jan Balster
Jania Hernández Dormuz
Janne Vanhala
Jason R. Coombs
Javier Domingo Cansino
Expand Down
1 change: 1 addition & 0 deletions changelog/15097.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:meth:`pytest.WarningsRecorder.pop` now returns the first matching warning when the matches have unrelated categories, instead of the last one.
22 changes: 12 additions & 10 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,19 @@ def pop(self, cls: type[Warning] = Warning) -> warnings.WarningMessage:
but not an instance of a child class of any other match.
Raises ``AssertionError`` if there is no match.
"""
best_idx: int | None = None
categories = {w.category for w in self._list if issubclass(w.category, cls)}
# Keep only the categories that are not a subclass of another match.
least_derived = {
category
for category in categories
if not any(
other is not category and issubclass(category, other)
for other in categories
)
}
for i, w in enumerate(self._list):
if w.category == cls:
return self._list.pop(i) # exact match, stop looking
if issubclass(w.category, cls) and (
best_idx is None
or not issubclass(w.category, self._list[best_idx].category)
):
best_idx = i
if best_idx is not None:
return self._list.pop(best_idx)
if w.category in least_derived:
return self._list.pop(i)
__tracebackhide__ = True
raise AssertionError(f"{cls!r} not found in warning list")

Expand Down
19 changes: 19 additions & 0 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class ChildWarning(ParentWarning):
class ChildOfChildWarning(ChildWarning):
pass

class SiblingWarning(ParentWarning):
pass

@staticmethod
def raise_warnings_from_list(_warnings: list[type[Warning]]):
for warn in _warnings:
Expand Down Expand Up @@ -82,6 +85,22 @@ def test_pop_finds_best_inexact_match(self):
_warn = record.pop(self.ParentWarning)
assert _warn.category is self.ChildWarning

def test_pop_unrelated_matches_returns_first(self):
with pytest.warns(self.ParentWarning) as record:
self.raise_warnings_from_list([self.ChildWarning, self.SiblingWarning])

_warn = record.pop(self.ParentWarning)
assert _warn.category is self.ChildWarning

def test_pop_unrelated_matches_prefers_less_derived(self):
with pytest.warns(self.ParentWarning) as record:
self.raise_warnings_from_list(
[self.ChildOfChildWarning, self.SiblingWarning, self.ChildWarning]
)

_warn = record.pop(self.ParentWarning)
assert _warn.category is self.SiblingWarning


class TestWarningsRecorderChecker:
def test_recording(self) -> None:
Expand Down
Loading