GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — contextlib.suppress does not support ExceptionGroups · Issue #103791 · python/cpython

contextlib.suppress does not support ExceptionGroups #103791

New issue
Copy link
New issue
Copy link
Closed
Closed
contextlib.suppress does not support ExceptionGroups#103791
Copy link
Labels
type-featureA feature request or enhancementA feature request or enhancement

Description

@ambv
ambv
opened on Apr 24, 2023
Issue body actions

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

  • gh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup #103792
  • gh-103791: handle BaseExceptionGroup in contextlib.suppress() #111910
  • [3.12] gh-103791: handle BaseExceptionGroup in contextlib.suppress() (GH-111910) #111955
Reacted by Lars Alexander Blumberg

Activity

  1. ambv
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Apr 24, 2023
  2. ambv
    added 2 commits that reference this issue on Apr 24, 2023

    pythongh-103791: Make contextlib.suppress also act on exceptions with…

    7bc3c15

    gh-103791: Make contextlib.suppress also act on exceptions within an …

    22bed58
  3. carljm
    added a commit that references this issue on Apr 24, 2023

    Merge branch 'main' into superopt_spec

    af807d6
  4. ambv
    closed this as completedon Apr 24, 2023
  5. iritkatriel
    added a commit that references this issue on Nov 10, 2023

    gh-103791: handle `BaseExceptionGroup` in `contextlib.suppress()` (#1…

    d61313b
  6. miss-islington
    added a commit that references this issue on Nov 10, 2023

    pythongh-103791: handle `BaseExceptionGroup` in `contextlib.suppress(…

    9e2096d
  7. iritkatriel
    added a commit that references this issue on Nov 10, 2023

    [3.12] gh-103791: handle `BaseExceptionGroup` in `contextlib.suppress…

    3780414
  8. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-103791: handle `BaseExceptionGroup` in `contextlib.suppress(…

    a2535e0
  9. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-103791: handle `BaseExceptionGroup` in `contextlib.suppress(…

    27cc8c8
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 enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.