Skip to content

Commit

Permalink
Further tweaks from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jul 4, 2023
1 parent c4876c7 commit 7775e49
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
write_to = "src/_pytest/_version.py"

[tool.pytest.ini_options]
#minversion = "2.0"
minversion = "2.0"
addopts = "-rfEX -p pytester --strict-markers"
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
python_classes = ["Test", "Acceptance"]
Expand Down
11 changes: 4 additions & 7 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,17 @@ def __len__(self) -> int:
return len(self._list)

def pop(self, cls: Type[Warning] = Warning) -> "warnings.WarningMessage":
"""Pop the first recorded warning which is an instance of ``cls``.
But not an instance of a child class of any other match.
"""Pop the first recorded warning which is an instance of ``cls``,
but not an instance of a child class of any other match.
Raises ``AssertionError`` if there is no match.
"""

best_idx = None
best_idx: Optional[int] = None
for i, w in enumerate(self._list):
if w.category == cls:
return self._list.pop(i) # exact match, stop looking
if issubclass(w.category, cls) and (
best_idx is None
or not issubclass(w.category, self._list[best_idx].category) # type: ignore[unreachable]
or not issubclass(w.category, self._list[best_idx].category)
):
best_idx = i
if best_idx is not None:
Expand Down
6 changes: 3 additions & 3 deletions testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def raise_warnings_from_list(_warnings: List[Type[Warning]]):
for warn in _warnings:
warnings.warn(f"Warning {warn().__repr__()}", warn)

def test_pop(self):
def test_pop_finds_exact_match(self):
with pytest.warns((self.ParentWarning, self.ChildWarning)) as record:
self.raise_warnings_from_list(
[self.ChildWarning, self.ParentWarning, self.ChildOfChildWarning]
Expand All @@ -64,13 +64,13 @@ def test_pop(self):
_warn = record.pop(self.ParentWarning)
assert _warn.category is self.ParentWarning

def test_pop_raises(self):
def test_pop_raises_if_no_match(self):
with pytest.raises(AssertionError):
with pytest.warns(self.ParentWarning) as record:
self.raise_warnings_from_list([self.ParentWarning])
record.pop(self.ChildOfChildWarning)

def test_pop_most_recent(self):
def test_pop_finds_best_inexact_match(self):
with pytest.warns(self.ParentWarning) as record:
self.raise_warnings_from_list(
[self.ChildOfChildWarning, self.ChildWarning, self.ChildOfChildWarning]
Expand Down

0 comments on commit 7775e49

Please sign in to comment.