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 — Subclasses of `ExceptionGroup` can wrap `BaseException`s · Issue #99553 · python/cpython

Subclasses of ExceptionGroup can wrap BaseExceptions #99553

New issue
Copy link
New issue
Copy link
Closed
#99572
Closed
Subclasses of ExceptionGroup can wrap BaseExceptions#99553
#99572
Copy link
Labels
3.11only security fixesonly security fixes3.12only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@Zac-HD
Zac-HD
opened on Nov 17, 2022
Issue body actions
class MyEG(ExceptionGroup):
    """Holds BaseExceptions without itself being a BaseException."""

oops = MyEG("oops", [KeyboardInterrupt()])
assert isinstance(oops, Exception)
assert not isinstance(oops.exceptions[0], Exception)

I believe that this is a bug tracing to the period when PEP-654 did not intend (Base)ExceptionGroup to be usable as a parent class; and I think a sufficient fix would be to replace the type-equality check with an isinstance check in:

cpython/Objects/exceptions.c

Lines 740 to 744 in bc390dd

if (cls == PyExc_ExceptionGroup) {
if (nested_base_exceptions) {
PyErr_SetString(PyExc_TypeError,
"Cannot nest BaseExceptions in an ExceptionGroup");
goto error;

cc @iritkatriel; raised via agronholm/exceptiongroup#40 (comment)

Linked PRs

  • gh-99553: fix bug where an ExceptionGroup subclass can wrap a BaseException #99572
  • [3.11] gh-99553: fix bug where an ExceptionGroup subclass can wrap a BaseException (GH-99572) #99580
  • gh-99553: add tests for ExceptionGroup wrapping #99615
  • [3.11] gh-99553: add tests for ExceptionGroup wrapping (GH-99615) #103435

Activity

  1. Zac-HD
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 17, 2022
  2. iritkatriel commented on Nov 17, 2022

    @iritkatriel
    iritkatriel
    on Nov 17, 2022
    Member
    More actions

    I believe we discussed this and decided that we leave it to the subclass author to deal with it. Maybe it needs to be documented (or reconsidered)?

    CC @gvanrossum @1st1

  3. Zac-HD commented on Nov 17, 2022

    @Zac-HD
    Zac-HD
    on Nov 17, 2022
    ContributorAuthor
    More actions

    I think at least documented, though I'd prefer it was changed.

    If people intend MyEG to contain BaseExceptions then the obvious way is to inherit from BaseExceptionGroup (and likely have a BaseMyEg; Trio has already shipped this pattern), so any cases where MyEG ends up containing e.g. a KeyboardInterrupt and then discarding that to an except Exception: would be unfortunate! I think the PEP argues against this well 🙂

    The == comparison also makes static typing very confusing: if we have BaseExceptionGroup -> ExceptionGroup -> MyEG and the last can also contain BaseExceptions, then either ExceptionGroup.exceptions must be annotated as able to contain BaseExceptions when it cannot, the annotations for MyEG must say it cannot when in fact it can, or we violate the substitution principle. This last is currently the case at runtime, but typecheckers are not keen on such things.

  4. Zac-HD commented on Nov 17, 2022

    @Zac-HD
    Zac-HD
    on Nov 17, 2022
    ContributorAuthor
    More actions

    Hah! It looks like I just find this persistently startling: #28569 (comment) (but I promise, no PEP this time 😉)

  5. gvanrossum commented on Nov 17, 2022

    @gvanrossum
    gvanrossum
    on Nov 17, 2022
    Member
    More actions

    Yeah, I definitely recall that we discussed this before. But looking at the code it's possible that we were too conservative. In particular in the fallback clause we could have chosen to check if cls is a subclass of PyExc_ExceptionGroup if nested_base_exceptions is set and raise the same error as on line 742.

    The thing we definitely can't do is adjusting the class if it is exactly PyExc_BaseExceptionGroup and nested_base_exceptions is false.

    The question is, can we treat this as a bug and fix it (in 3.11.1 if it isn't out yet) or would such a fix be considered backwards compatible?

  6. iritkatriel commented on Nov 17, 2022

    @iritkatriel
    iritkatriel
    on Nov 17, 2022
    Member
    More actions

    I think we can call it a bug. It’s better to change it in 3.11.1 than in 3.12.

    Reacted by Zac Hatfield-Dodds
  7. gvanrossum commented on Nov 17, 2022

    @gvanrossum
    gvanrossum
    on Nov 17, 2022
    Member
    More actions

    Sounds good.

  8. iritkatriel
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    3.11only security fixesonly security fixes
    3.12only security fixesonly security fixes
    on Nov 18, 2022
  9. iritkatriel
    added a commit that references this issue on Nov 18, 2022

    pythongh-99553: fix bug where an ExceptionGroup subclass can wrap a B…

    da98a62
  10. iritkatriel
    closed this as completedin #99572on Nov 18, 2022
  11. iritkatriel
    added a commit that references this issue on Nov 18, 2022

    gh-99553: fix bug where an ExceptionGroup subclass can wrap a BaseExc…

    c8c6113
  12. miss-islington
    added 2 commits that reference this issue on Nov 18, 2022

    pythongh-99553: fix bug where an ExceptionGroup subclass can wrap a B…

    65c8f88

    gh-99553: fix bug where an ExceptionGroup subclass can wrap a BaseExc…

    82ab9e6
  13. Zac-HD
    added a commit that references this issue on Nov 20, 2022

    pythongh-99553: clarify doc and add tests

    4b9e01a
  14. hauntsaninja
    added a commit that references this issue on Apr 11, 2023

    gh-99553: add tests for ExceptionGroup wrapping (#99615)

    4cd1cc8
  15. miss-islington
    added 2 commits that reference this issue on Apr 11, 2023

    pythongh-99553: add tests for ExceptionGroup wrapping (pythonGH-99615)

    0fd85aa

    gh-99553: add tests for ExceptionGroup wrapping (GH-99615)

    254494c
  16. warsaw
    added a commit that references this issue on Apr 11, 2023

    pythongh-99553: add tests for ExceptionGroup wrapping (python#99615)

    18df988
  17. aisk
    added a commit that references this issue on Apr 18, 2023

    pythongh-99553: add tests for ExceptionGroup wrapping (python#99615)

    dd9946d
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

    3.11only security fixesonly security fixes3.12only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    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.