-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed
Labels
type-featureA feature request or enhancementA feature request or enhancement
Description
Currently, contextlib.suppress does not successfully suppress exceptions that are wrapped within an ExceptionGroup.
In other words, this works:
def raise_ve():
raise ValueError("ve")
with suppress(ValueError):
raise_ve()while this doesn't:
def raise_ve_eg():
raise ExceptionGroup("eg", [ValueError("ve")])
with suppress(ValueError):
raise_ve_eg()The user's intent is to suppress the latter case, too. It should work just as well, removing ValueError instances from the exception group. If it ends up empty, nothing gets raised. Otherwise, an ExceptionGroup should be re-raised with the remaining exceptions. For instance:
def raise_ve_eg3():
raise ExceptionGroup("eg", [ValueError("ve1"), KeyError("ke"), ValueError("ve2")])
with suppress(ValueError):
raise_ve_eg3()should raise an ExceptionGroup with the KeyError("ke") only, as we suppressed ValueError instances.
Linked PRs
larsblumberg
Metadata
Metadata
Assignees
Labels
type-featureA feature request or enhancementA feature request or enhancement