fix(warnings-recorder): Match also subclass of warning in pop
This commit is contained in:
parent
18e87c9831
commit
8ac3c645fa
|
@ -206,12 +206,18 @@ class WarningsRecorder(warnings.catch_warnings): # type:ignore[type-arg]
|
||||||
return len(self._list)
|
return len(self._list)
|
||||||
|
|
||||||
def pop(self, cls: Type[Warning] = Warning) -> "warnings.WarningMessage":
|
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):
|
for i, w in enumerate(self._list):
|
||||||
if issubclass(w.category, cls):
|
if w.category == cls:
|
||||||
return self._list.pop(i)
|
return self._list.pop(i)
|
||||||
|
if issubclass(w.category, cls):
|
||||||
|
matches.append((i, w))
|
||||||
|
if not matches:
|
||||||
__tracebackhide__ = True
|
__tracebackhide__ = True
|
||||||
raise AssertionError(f"{cls!r} not found in warning list")
|
raise AssertionError(f"{cls!r} not found in warning list")
|
||||||
|
(idx, best), *rest = matches
|
||||||
|
return self._list.pop(idx)
|
||||||
|
|
||||||
def clear(self) -> None:
|
def clear(self) -> None:
|
||||||
"""Clear the list of recorded warnings."""
|
"""Clear the list of recorded warnings."""
|
||||||
|
|
Loading…
Reference in New Issue