Skip to content

contextlib.suppress does not support ExceptionGroups #103791

Description

@ambv

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions