-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closed
Labels
type: proposalproposal for a new feature, often to gather opinions or design the API around the new featureproposal for a new feature, often to gather opinions or design the API around the new feature
Description
What's the problem this feature will solve?
Let pytest.raises() handle ExceptionGroup unwrapping, similar to what an "except*" clause would do. The following is valid python code that will catch the ValueError exception:
try:
raise ExceptionGroup("", [ValueError])
except* ValueError:
pass
However pytest.raises will not manage this, and will fail with catching the ExceptionGroup instead.
with pytest.raises(ValueError):
raise ExceptionGroup("", [ValueError])
Describe the solution you'd like
Assert some code throws an exception or an exception as part of an exception group.
anyio/trio will now always wrap exception in ExceptionGroups when raised from taskgroups. This leads to silly checks like: https://github.com/agronholm/anyio/blob/3f1eca1addcd782e2347350a6ddb2ad2b80c6354/tests/test_taskgroups.py#L279C1-L287C31 to unwrap the exceptiongroup.
A basic solution to handle this (without the bells and whistles) would be:
@contextlib.contextmanager
def raises_nested(exc):
try:
yield
except* exc:
pass
else:
raise AssertionError("Did not raise expected exception")
Alternative Solutions
Additional context
bdraco, edenhaus, pquentin, sergei3000, eNcacz and 9 more
Metadata
Metadata
Assignees
Labels
type: proposalproposal for a new feature, often to gather opinions or design the API around the new featureproposal for a new feature, often to gather opinions or design the API around the new feature
Type
Projects
Status
Done