8000 `contextlib.suppress` converts instances of a subtype of `ExceptionGroup` to an instance of the `ExceptionGroup` class · Issue #119287 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

contextlib.suppress converts instances of a subtype of ExceptionGroup to an instance of the ExceptionGroup class #119287

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

Closed
DetachHead opened this issue May 20, 2024 · 2 comments · Fixed by #119657
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes docs Documentation in the Doc dir

Comments

@DetachHead
Copy link
DetachHead commented May 20, 2024

Bug report

Bug description:

from contextlib import suppress

class FooException(Exception): ...

class FooExceptionGroup(ExceptionGroup[Exception]): ...

try:
    with suppress(FooException):
        raise FooExceptionGroup("", [Exception()])
except ExceptionGroup as e:
    print(type(e))

in this code, the suppress context manager is expected to have no effect, as a FooException is not being raised within it. instead, it gets converted to an ExceptionGroup then re-raised.

expected output:

<class '__main__.FooExceptionGroup'>

actual output:

<class 'ExceptionGroup'>

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

@DetachHead DetachHead added the type-bug An unexpected behavior, bug, or error label May 20, 2024
@AlexWaygood AlexWaygood changed the title contextlib.suppress converts instances of a suptype of ExceptionGroup to an instance of the ExceptionGroup class contextlib.suppress converts instances of a subtype of ExceptionGroup to an instance of the ExceptionGroup class May 21, 2024
@TeamSpen210
Copy link
8000

For ExceptionGroup subclasses, you need to implement the derive() method, so that other code knows how to create a new exception group. It can't call the class since yours could have additional required arguments. If you do that then you'll get back your subclass.

@DetachHead
Copy link
Author

thanks, that seems to work. however i think this behavior should be documented either in the docs for the derive method (which currently only mentions that it's used by the split and subgroup methods, but not contextlib.suppress), or in the docs for contextlib.suppress (which mentions that ExceptionGroups are supported as of python 3.12, but not that the derive method is required for it to work correctly)

@iritkatriel iritkatriel added docs Documentation in the Doc dir needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes and removed type-bug An unexpected behavior, bug, or error labels May 23, 2024
@hugovk hugovk added 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes and removed needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels May 23, 2024
iritkatriel added a commit to iritkatriel/cpython that referenced this issue May 28, 2024
iritkatriel added a commit that referenced this issue Jun 5, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 5, 2024
… it from contextlib.suppress (pythonGH-119657)

(cherry picked from commit 5c02ea8)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 5, 2024
… it from contextlib.suppress (pythonGH-119657)

(cherry picked from commit 5c02ea8)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
iritkatriel added a commit that referenced this issue Jun 5, 2024
…o it from contextlib.suppress (GH-119657) (#120106)

gh-119287: clarify doc on BaseExceptionGroup.derive and link to it from contextlib.suppress (GH-119657)
(cherry picked from commit 5c02ea8)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
iritkatriel added a commit that referenced this issue Jun 5, 2024
…o it from contextlib.suppress (GH-119657) (#120105)

gh-119287: clarify doc on BaseExceptionGroup.derive and link to it from contextlib.suppress (GH-119657)
(cherry picked from commit 5c02ea8)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
barneygale pushed a commit to barneygale/cpython that referenced this issue Jun 5, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes docs Documentation in the Doc dir
Projects
None yet
4 participants
0