Skip to content

Add ordered parameter to RaisesGroup#14594

Open
anneheartrecord wants to merge 1 commit into
pytest-dev:mainfrom
anneheartrecord:feat/14580-raisesgroup-ordered
Open

Add ordered parameter to RaisesGroup#14594
anneheartrecord wants to merge 1 commit into
pytest-dev:mainfrom
anneheartrecord:feat/14580-raisesgroup-ordered

Conversation

@anneheartrecord

Copy link
Copy Markdown

Summary

Closes #14580.

pytest.RaisesGroup currently ignores the order of the exceptions in a group and pairs them up with a greedy matching algorithm. This PR adds an opt-in ordered: bool = False parameter. When ordered=True, the expected exceptions are matched against the raised exceptions positionally (expected[i] vs actual[i]), which:

  • lets you assert that exceptions were raised in a particular order (the feature request in Support checking exception order in RaisesGroup #14580), and
  • side-steps the documented greedy-algorithm pitfall, where a valid pairing exists but the greedy algorithm fails to find it.

The default (ordered=False) preserves the existing order-insensitive behavior, so this is fully backwards compatible.

Example

# order is asserted
with pytest.RaisesGroup(ValueError, TypeError, ordered=True):
    raise ExceptionGroup("msg", [ValueError("foo"), TypeError("bar")])

# wrong order now fails
with pytest.RaisesGroup(ValueError, TypeError, ordered=True):
    raise ExceptionGroup("msg", [TypeError("bar"), ValueError("foo")])
# -> Raised exception group did not match: At index 0: `TypeError()` is not an instance of `ValueError`

Implementation notes

  • Added ordered to all RaisesGroup.__init__ overloads and the runtime signature, stored as self.ordered.
  • New focused helper RaisesGroup._check_exceptions_ordered does the positional matching and produces failure messages that point at the offending index (At index N: ...) and report length mismatches.
  • RaisesGroup._check_exceptions short-circuits to the ordered path when ordered=True; the greedy path is untouched.
  • __repr__ shows ordered=True only when set (parallel to flatten_subgroups).
  • Docstring, doc/en/how-to/assert.rst, and a changelog/14580.feature.rst newsfragment updated; added myself to AUTHORS.

Testing

  • testing/python/raises_group.py: new test_ordered (success in/out of order, wrong order, wrong length, nested groups + RaisesExc, greedy-pitfall case) and test_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 --check clean on all touched files.

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.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support checking exception order in RaisesGroup

1 participant