From 8ac3c645fa4a87afb30295d28e8d6fb67166bd01 Mon Sep 17 00:00:00 2001 From: Lesnek Date: Sun, 2 Jul 2023 14:17:06 +0200 Subject: [PATCH] fix(warnings-recorder): Match also subclass of warning in pop --- src/_pytest/recwarn.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index ff8e70820..ba5a3311c 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -206,12 +206,18 @@ class WarningsRecorder(warnings.catch_warnings): # type:ignore[type-arg] return len(self._list) def pop(self, cls: Type[Warning] = Warning) -> "warnings.WarningMessage": - """Pop the first recorded warning, raise exception if not exists.""" + """Pop the first recorded warning (or subclass of warning), raise exception if not exists.""" + matches = [] for i, w in enumerate(self._list): - if issubclass(w.category, cls): + if w.category == cls: return self._list.pop(i) - __tracebackhide__ = True - raise AssertionError(f"{cls!r} not found in warning list") + if issubclass(w.category, cls): + matches.append((i, w)) + if not matches: + __tracebackhide__ = True + raise AssertionError(f"{cls!r} not found in warning list") + (idx, best), *rest = matches + return self._list.pop(idx) def clear(self) -> None: """Clear the list of recorded warnings."""