Skip to content

Commit ef8b299

Browse files
authored
Merge pull request #14620 from pytest-dev/patchback/backports/9.1.x/680f9f3ed3faaaa9a4fd1d8120618cd17cdee2d0/pr-14220
[PR #14220/680f9f3e backport][9.1.x] raises: fix stale loop variable in RaisesGroup error reporting
2 parents 79fbf93 + 66abd07 commit ef8b299

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

changelog/14220.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a logic bug in :class:`pytest.RaisesGroup` which would might cause it to display incorrect "It matches `FooError()` which was paired with `BarError`" messages.

src/_pytest/raises.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,7 @@ def _check_exceptions(
13541354
f"\n{indent_1}{self._repr_expected(self.expected_exceptions[i_failed])}"
13551355
)
13561356
for i_actual, actual in enumerate(actual_exceptions):
1357-
if results.get_result(i_exp, i_actual) is None:
1357+
if results.get_result(i_failed, i_actual) is None:
13581358
# we print full repr of match target
13591359
s += (
13601360
f"\n{indent_2}It matches {backquote(repr(actual))} which was paired with "

testing/python/raises_group.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,3 +1350,21 @@ def test_tuples() -> None:
13501350
),
13511351
):
13521352
RaisesGroup((ValueError, IndexError)) # type: ignore[call-overload]
1353+
1354+
1355+
def test_expected_matching_only_on_matching() -> None:
1356+
"""Regression test for #14220, logic error which caused the "which was
1357+
paired with" message to appear for wrong pairs."""
1358+
with (
1359+
fails_raises_group(
1360+
"\n"
1361+
"1 matched exception. \n"
1362+
"Too few exceptions raised!\n"
1363+
"The following expected exceptions did not find a match:\n"
1364+
" ValueError\n"
1365+
" TypeError\n"
1366+
" It matches `TypeError()` which was paired with `TypeError`",
1367+
),
1368+
RaisesGroup(TypeError, ValueError, TypeError),
1369+
):
1370+
raise ExceptionGroup("", [TypeError()])

0 commit comments

Comments
 (0)