Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contextlib.suppress does not support ExceptionGroups #103791

Closed
ambv opened this issue Apr 24, 2023 · 0 comments
Closed

contextlib.suppress does not support ExceptionGroups #103791

ambv opened this issue Apr 24, 2023 · 0 comments
Labels
type-feature A feature request or enhancement

Comments

@ambv
Copy link
Contributor

ambv commented Apr 24, 2023

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

@ambv ambv added the type-feature A feature request or enhancement label Apr 24, 2023
ambv added a commit to ambv/cpython that referenced this issue Apr 24, 2023
ambv added a commit that referenced this issue Apr 24, 2023
…ExceptionGroup (#103792)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
carljm added a commit to carljm/cpython that referenced this issue Apr 24, 2023
* main:
  pythongh-87729: add LOAD_SUPER_ATTR instruction for faster super() (python#103497)
  pythongh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup (python#103792)
@ambv ambv closed this as completed Apr 24, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 10, 2023
…)` (pythonGH-111910)

(cherry picked from commit d61313b)

Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
iritkatriel pushed a commit that referenced this issue Nov 10, 2023
…()` (GH-111910) (#111955)

gh-103791: handle `BaseExceptionGroup` in `contextlib.suppress()` (GH-111910)
(cherry picked from commit d61313b)

Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant