Skip to content

Comments

raises: fix stale loop variable in RaisesGroup error reporting#14220

Open
bysiber wants to merge 3 commits intopytest-dev:mainfrom
bysiber:fix-stale-iexp-raisesgroup
Open

raises: fix stale loop variable in RaisesGroup error reporting#14220
bysiber wants to merge 3 commits intopytest-dev:mainfrom
bysiber:fix-stale-iexp-raisesgroup

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

In RaisesGroup._check_exceptions, the diagnostic section that builds failure messages for unmatched expected exceptions has a bug on this line:

for i_failed in failed_expected:
    s += (
        f"\n{indent_1}{self._repr_expected(self.expected_exceptions[i_failed])}"
    )
    for i_actual, actual in enumerate(actual_exceptions):
        if results.get_result(i_exp, i_actual) is None:  # <-- bug

i_exp here is a leftover from the earlier for i_exp, expected in enumerate(self.expected_exceptions): loop that computes the full results matrix. By the time we reach this diagnostic loop, i_exp holds whatever value it had when that earlier loop finished -- it doesn't track the current i_failed iteration at all.

This means the results.get_result(i_exp, i_actual) call looks up the wrong expected exception's result, so the "It matches ... which was paired with ..." hints in the failure output can be completely wrong or missing.

Fix: replace i_exp with i_failed, which is the actual loop variable for the current expected exception being reported.

In _check_exceptions, the diagnostic loop at line 1368 uses i_exp
(last value from the earlier enumerate loop) instead of i_failed
(the current loop variable). This causes incorrect result lookups
when building the failure message for unmatched expected exceptions.
@The-Compiler
Copy link
Member

Thanks! Could you please add a regression test to catch this issue?

bysiber and others added 2 commits February 20, 2026 15:47
Tests that the error message for failed expected exceptions uses the
correct index (i_failed) rather than a stale i_exp from a prior loop.
@bysiber
Copy link
Author

bysiber commented Feb 20, 2026

Added a regression test that exercises the error message path with multiple expected exceptions where a non-last expected fails. The test verifies the correct exception types appear in the failure output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants