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.
Previous PR's: #11656 (ExpectedExceptionGroup), #11671 (closed to give this a fresh PR w/o stale comments)
fixes: #11538 #12504
This adds
RaisesGroup
(also exported asraises_group
) andMatcher
, to allow a robust way of expectingExceptionGroup
and the ability to specify the structure of nested groups, etc etc.This is the exact implementation available in trio.testing.RaisesGroup which has been in use since december 2023 and gone through a couple iterations since to settle on a good interface. The most recent addition was python-trio/trio#3145 which added error messages, where the formatting can probably still be fine-tuned.
Pytest currently has excinfo.group_contains for checking exceptiongroups... which is very problematic. I think it should be deprecated, either in the same release as this PR, or very soon after.
This currently does not touch the implementation of
pytest.raises
, but if we madeMatcher
a contextmanager we could easily supportthis would fix e.g. #12763 with Matcher having the
check
parameter. The only complication is the legacy form of pytest.raises but that should be solvable with a small helper function.This can however be done in a followup PR.
This currently does not add an
.assert_matches
method, which previous iterations of the PR had. The reason to add that would be to improve the error message on failure, though it's possible to get the same result withthough note that
.matches()
is already a fairly niche feature that is entirely separate to the context-manager use (not to be confused with thematch
parameter).TODO: