Skip to content

Commit c4876c7

Browse files
committed
chore(CR): Add changes from code review
1 parent 6badb6f commit c4876c7

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
1010
write_to = "src/_pytest/_version.py"
1111

1212
[tool.pytest.ini_options]
13-
minversion = "2.0"
13+
#minversion = "2.0"
1414
addopts = "-rfEX -p pytester --strict-markers"
1515
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
1616
python_classes = ["Test", "Acceptance"]

src/_pytest/recwarn.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,26 @@ 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 (or subclass of warning), raise exception if not exists."""
210-
matches = []
209+
"""Pop the first recorded warning which is an instance of ``cls``.
210+
211+
But not an instance of a child class of any other match.
212+
Raises ``AssertionError`` if there is no match.
213+
214+
"""
215+
216+
best_idx = None
211217
for i, w in enumerate(self._list):
212218
if w.category == cls:
213-
return self._list.pop(i)
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-
for i, w in rest:
221-
if issubclass(w.category, best.category) and not issubclass(
222-
best.category, w.category
219+
return self._list.pop(i) # exact match, stop looking
220+
if issubclass(w.category, cls) and (
221+
best_idx is None
222+
or not issubclass(w.category, self._list[best_idx].category) # type: ignore[unreachable]
223223
):
224-
idx, best = i, w
225-
return self._list.pop(idx)
224+
best_idx = i
225+
if best_idx is not None:
226+
return self._list.pop(best_idx)
227+
__tracebackhide__ = True
228+
raise AssertionError(f"{cls!r} not found in warning list")
226229

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

testing/test_recwarn.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ def test_pop_raises(self):
7272

7373
def test_pop_most_recent(self):
7474
with pytest.warns(self.ParentWarning) as record:
75-
self.raise_warnings_from_list([self.ChildWarning, self.ChildOfChildWarning])
75+
self.raise_warnings_from_list(
76+
[self.ChildOfChildWarning, self.ChildWarning, self.ChildOfChildWarning]
77+
)
7678

7779
_warn = record.pop(self.ParentWarning)
78-
assert _warn.category is self.ChildOfChildWarning
80+
assert _warn.category is self.ChildWarning
7981

8082

8183
class TestWarningsRecorderChecker:

0 commit comments

Comments
 (0)