chore(CR): Add changes from code review

This commit is contained in:
Lesnek 2023-07-04 10:20:50 +02:00
parent 6badb6f01e
commit c4876c7106
3 changed files with 22 additions and 17 deletions

View File

@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
write_to = "src/_pytest/_version.py" write_to = "src/_pytest/_version.py"
[tool.pytest.ini_options] [tool.pytest.ini_options]
minversion = "2.0" #minversion = "2.0"
addopts = "-rfEX -p pytester --strict-markers" addopts = "-rfEX -p pytester --strict-markers"
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"] python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
python_classes = ["Test", "Acceptance"] python_classes = ["Test", "Acceptance"]

View File

@ -206,23 +206,26 @@ 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 (or subclass of warning), raise exception if not exists.""" """Pop the first recorded warning which is an instance of ``cls``.
matches = []
But not an instance of a child class of any other match.
Raises ``AssertionError`` if there is no match.
"""
best_idx = None
for i, w in enumerate(self._list): for i, w in enumerate(self._list):
if w.category == cls: if w.category == cls:
return self._list.pop(i) return self._list.pop(i) # exact match, stop looking
if issubclass(w.category, cls): if issubclass(w.category, cls) and (
matches.append((i, w)) best_idx is None
if not matches: or not issubclass(w.category, self._list[best_idx].category) # type: ignore[unreachable]
):
best_idx = i
if best_idx is not None:
return self._list.pop(best_idx)
__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
for i, w in rest:
if issubclass(w.category, best.category) and not issubclass(
best.category, w.category
):
idx, best = i, w
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."""

View File

@ -72,10 +72,12 @@ class TestSubclassWarningPop:
def test_pop_most_recent(self): def test_pop_most_recent(self):
with pytest.warns(self.ParentWarning) as record: with pytest.warns(self.ParentWarning) as record:
self.raise_warnings_from_list([self.ChildWarning, self.ChildOfChildWarning]) self.raise_warnings_from_list(
[self.ChildOfChildWarning, self.ChildWarning, self.ChildOfChildWarning]
)
_warn = record.pop(self.ParentWarning) _warn = record.pop(self.ParentWarning)
assert _warn.category is self.ChildOfChildWarning assert _warn.category is self.ChildWarning
class TestWarningsRecorderChecker: class TestWarningsRecorderChecker: