Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/14327.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``RaisesGroup.matches()`` so group-level ``check=`` callbacks are no longer called again on the first nested exception while building suggestion text.
8 changes: 4 additions & 4 deletions src/_pytest/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,12 +1210,12 @@ def matches(
if (
len(actual_exceptions) == len(self.expected_exceptions) == 1
and isinstance(expected := self.expected_exceptions[0], type)
# we explicitly break typing here :)
and self._check_check(actual_exceptions[0]) # type: ignore[arg-type]
and isinstance(actual_exceptions[0], expected)
):
self._fail_reason = reason + (
f", but did return True for the expected {self._repr_expected(expected)}."
f" You might want RaisesGroup(RaisesExc({expected.__name__}, check=<...>))"
f". If you meant to check the sub-exception instead of the group,"
f" you might want "
f"RaisesGroup(RaisesExc({expected.__name__}, check=<...>))"
)
else:
self._fail_reason = reason
Expand Down
40 changes: 38 additions & 2 deletions testing/python/raises_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def is_exc(e: ExceptionGroup[ValueError]) -> bool:

with (
fails_raises_group(
f"check {is_exc_repr} did not return True on the ExceptionGroup"
f"check {is_exc_repr} did not return True on the ExceptionGroup. If you meant to check the sub-exception instead of the group, you might want RaisesGroup(RaisesExc(ValueError, check=<...>))"
),
RaisesGroup(ValueError, check=is_exc),
):
Expand All @@ -429,13 +429,49 @@ def is_value_error(e: BaseException) -> bool:
# helpful suggestion if the user thinks the check is for the sub-exception
with (
fails_raises_group(
f"check {is_value_error} did not return True on the ExceptionGroup, but did return True for the expected ValueError. You might want RaisesGroup(RaisesExc(ValueError, check=<...>))"
f"check {is_value_error} did not return True on the ExceptionGroup. If you meant to check the sub-exception instead of the group, you might want RaisesGroup(RaisesExc(ValueError, check=<...>))"
),
RaisesGroup(ValueError, check=is_value_error),
):
raise ExceptionGroup("", (ValueError(),))


def test_check_is_only_called_on_the_group() -> None:
calls: list[type[BaseException]] = []

def check(exc_group: ExceptionGroup[ValueError]) -> bool:
calls.append(type(exc_group))
return False

with (
fails_raises_group(
f"check {repr_callable(check)} did not return True on the ExceptionGroup. If you meant to check the sub-exception instead of the group, you might want RaisesGroup(RaisesExc(ValueError, check=<...>))"
),
RaisesGroup(ValueError, check=check),
):
raise ExceptionGroup("", (ValueError(),))

assert calls == [ExceptionGroup]


def test_matches_check_is_only_called_on_the_group() -> None:
calls: list[type[BaseException]] = []

def check(exc_group: ExceptionGroup[ValueError]) -> bool:
calls.append(type(exc_group))
return False

matcher = RaisesGroup(ValueError, check=check)

assert not matcher.matches(ExceptionGroup("", (ValueError(),)))
assert matcher.fail_reason == (
f"check {repr_callable(check)} did not return True on the ExceptionGroup. "
"If you meant to check the sub-exception instead of the group, you might "
"want RaisesGroup(RaisesExc(ValueError, check=<...>))"
)
assert calls == [ExceptionGroup]


def test_unwrapped_match_check() -> None:
def my_check(e: object) -> bool: # pragma: no cover
return True
Expand Down
Loading