Add ordered parameter to RaisesGroup#14594
Open
anneheartrecord wants to merge 1 commit into
Open
Conversation
By default RaisesGroup ignores the order of the exceptions in the group and uses a greedy matching algorithm. The new ordered=True parameter matches the expected exceptions against the raised exceptions positionally, asserting the order and avoiding the greedy-algorithm pitfall where a valid pairing exists but is not found.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #14580.
pytest.RaisesGroupcurrently ignores the order of the exceptions in a group and pairs them up with a greedy matching algorithm. This PR adds an opt-inordered: bool = Falseparameter. Whenordered=True, the expected exceptions are matched against the raised exceptions positionally (expected[i] vs actual[i]), which:RaisesGroup#14580), andThe default (
ordered=False) preserves the existing order-insensitive behavior, so this is fully backwards compatible.Example
Implementation notes
orderedto allRaisesGroup.__init__overloads and the runtime signature, stored asself.ordered.RaisesGroup._check_exceptions_ordereddoes the positional matching and produces failure messages that point at the offending index (At index N: ...) and report length mismatches.RaisesGroup._check_exceptionsshort-circuits to the ordered path whenordered=True; the greedy path is untouched.__repr__showsordered=Trueonly when set (parallel toflatten_subgroups).doc/en/how-to/assert.rst, and achangelog/14580.feature.rstnewsfragment updated; added myself toAUTHORS.Testing
testing/python/raises_group.py: newtest_ordered(success in/out of order, wrong order, wrong length, nested groups +RaisesExc, greedy-pitfall case) andtest_ordered_repr.testing/typing_raises_group.py:ordered=accepted alongside the other parameters.uv run pytest testing/python/raises_group.py testing/python/raises.py→ 67 passed; with--doctest-modules src/_pytest/raises.py→ 68 passed.ruff check/ruff format --checkclean on all touched files.