raises: fix stale loop variable in RaisesGroup error reporting by bysiber · Pull Request #14220 · pytest-dev/pytest
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.