Skip to content

Commit 8ac3c64

Browse files
committed
fix(warnings-recorder): Match also subclass of warning in pop
1 parent 18e87c9 commit 8ac3c64

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/_pytest/recwarn.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,18 @@ def __len__(self) -> int:
206206
return len(self._list)
207207

208208
def pop(self, cls: Type[Warning] = Warning) -> "warnings.WarningMessage":
209-
"""Pop the first recorded warning, raise exception if not exists."""
209+
"""Pop the first recorded warning (or subclass of warning), raise exception if not exists."""
210+
matches = []
210211
for i, w in enumerate(self._list):
211-
if issubclass(w.category, cls):
212+
if w.category == cls:
212213
return self._list.pop(i)
213-
__tracebackhide__ = True
214-
raise AssertionError(f"{cls!r} not found in warning list")
214+
if issubclass(w.category, cls):
215+
matches.append((i, w))
216+
if not matches:
217+
__tracebackhide__ = True
218+
raise AssertionError(f"{cls!r} not found in warning list")
219+
(idx, best), *rest = matches
220+
return self._list.pop(idx)
215221

216222
def clear(self) -> None:
217223
"""Clear the list of recorded warnings."""

0 commit comments

Comments
 (0)