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

bpo-45292: [PEP 654] add the ExceptionGroup and BaseExceptionGroup classes #28569

Merged
merged 40 commits into from
Oct 22, 2021

Conversation

iritkatriel
Copy link
Member

@iritkatriel iritkatriel commented Sep 26, 2021

This adds the ExceptionGroup and BaseExceptionGroup classes, but does not yet update the traceback display code to work with them correctly.

https://bugs.python.org/issue45292

@iritkatriel iritkatriel requested a review from a team as a code owner September 26, 2021 14:32
@iritkatriel iritkatriel added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 26, 2021
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @iritkatriel for commit c26ff6b38333ab6c990791628f827c5ab97998c2 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 26, 2021
@iritkatriel iritkatriel changed the title bpo-45292: [PEP 654] added ExceptionGroup and BaseExceptionGroup (did… bpo-45292: [PEP 654] add ExceptionGroup and BaseExceptionGroup (did… Sep 26, 2021
@iritkatriel iritkatriel changed the title bpo-45292: [PEP 654] add ExceptionGroup and BaseExceptionGroup (did… bpo-45292: [PEP 654] add ExceptionGroup and BaseExceptionGroup Sep 26, 2021
@iritkatriel iritkatriel changed the title bpo-45292: [PEP 654] add ExceptionGroup and BaseExceptionGroup bpo-45292: [PEP 654] add the ExceptionGroup and BaseExceptionGroup classes Sep 26, 2021
@pablogsal pablogsal added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 26, 2021
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @pablogsal for commit c26ff6b38333ab6c990791628f827c5ab97998c2 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Sep 26, 2021
@iritkatriel
Copy link
Member Author

This is part 1 - it rebased very easily so not much has changed in the area since April.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure you run the buildbot for leak checks.

I'd recommend asking for a review from a core dev who is better than me at catching subtle mistakes in C code (Serhiy?).

Lib/test/test_pickle.py Outdated Show resolved Hide resolved
Lib/test/test_pickle.py Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
@iritkatriel
Copy link
Member Author

Make sure you run the buildbot for leak checks.

Done.

I'd recommend asking for a review from a core dev who is better than me at catching subtle mistakes in C code (Serhiy?).

Would be great to have a review from @serhiy-storchaka if he's got the time and interest, and also @vstinner and @ncoghlan .

Lib/test/test_exception_group.py Show resolved Hide resolved
Lib/test/test_exception_group.py Outdated Show resolved Hide resolved
Comment on lines 79 to 81
self.assertIs(
type(MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])), MyEG)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also seems weird to me: I'd expect that if I want MyEG to hold a BaseException, I'd better create a MyBaseEG to do so, ala:

class MyBEG(BaseExceptionGroup):
    @staticmethod
    def create(msg, excs):
        # per creation suggestion above
        if all(isinstance(e, Exception) for e in excs):
            return MyEG(msg, excs)
        return MyBEG(msg, excs)

class MyEG(MyBEG, ExceptionGroup):
    pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For user-defined subclasses, we have to punt on the special behavior. And yes, user classes should probably do what you show, but we don't want to enforce it. (That would be making assumptions.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

IMO defining custom ExceptionGroup subclasses should be very rare anyway (maybe just Hypothesis and Trio?), and we can certainly deal with whatever complexity that requires.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, why would Hypothesis need to subclass EG? While we were called on arbitrarily forbidding subclassing (as we did in an earlier PEP draft) I don't see why it would be useful. What is your use case? Maybe there's something we can add to the design to avoid the need? (For Trio I could imagine they'd want to provide some kind of backwards compatibility with Trio's current solution.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a subclass to annotate the nested exceptions? See https://discuss.python.org/t/accepting-pep-654-exception-groups-and-except/10813/9

Copy link
Contributor

@Zac-HD Zac-HD Oct 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to change the exception type if we can avoid it, but the more important objection is that it complicates the reporting solely for implementation reasons:

class Explanation(Exception):
    __module__ = "builtins"
    def __str__(self) -> str:
        return f"\n{self.args[0]}"

try:
    why = "Failed!"
    raise AssertionError(why)
except Exception as e:
    msg = "    You can reproduce this error by ...\n    ..."
    raise Explanation(msg) from e

    # Ideally something more like:
    e.__note__ = msg
    raise
$ python example.py
Traceback (most recent call last):
  File "example.py", line 8, in <module>
    raise AssertionError(why)
AssertionError: Failed!
                                                                        # These lines are
The above exception was the direct cause of the following exception:    # confusing for new 
                                                                        # users, and they
Traceback (most recent call last):                                      # only exist due 
  File "example.py", line 10, in <module>                               # to implementation
    raise Explanation(msg) from e                                       # constraints :-(
Explanation:                                                            # 
    You can reproduce this error by ...
    ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that does not look very principled. Why make it possible to print the __note__ when an exception is printed as the child of an EG but not when it's printed at the toplevel? Maybe Hypotheses could wrap exceptions in some kind of proxy exception?

I'm assuming that the note describes the leaf's membership in the EG rather than the leaf itself. If that's not the case I agree it doesn't make sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The note is a description of the leaf, which could equally well be used for a single bare exception (though print() suffices).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like the requirement is to add a feature to BaseException to enrich exceptions in a way that shows up in their __str__.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created bpo45607 to track this request from Zac.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Buffer flush.)

Lib/test/test_pickle.py Outdated Show resolved Hide resolved
Lib/test/test_exception_group.py Show resolved Hide resolved
Comment on lines 79 to 81
self.assertIs(
type(MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])), MyEG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, why would Hypothesis need to subclass EG? While we were called on arbitrarily forbidding subclassing (as we did in an earlier PEP draft) I don't see why it would be useful. What is your use case? Maybe there's something we can add to the design to avoid the need? (For Trio I could imagine they'd want to provide some kind of backwards compatibility with Trio's current solution.)

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iritkatriel, have you managed to convince another core dev to review exceptions.c yet?

Lib/test/test_pickle.py Outdated Show resolved Hide resolved
Comment on lines 79 to 81
self.assertIs(
type(MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])), MyEG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll bite. What would you like to see supported upstream? A separate metadata item for each sub-exception? I'd say if you don't want to store that on the sub-exception in some dunder-ish attribute, you could indeed subclass EG to add that. There's an API so you can replicate this whenever an EG is being filtered. But I don't think we're going to support metadata that automatically gets replicated based on just this example -- in most cases I suspect you can just store an extra attribute on the individual exceptions.

@iritkatriel
Copy link
Member Author

@iritkatriel, have you managed to convince another core dev to review exceptions.c yet?

Not that I know of.

Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
Objects/exceptions.c Outdated Show resolved Hide resolved
@iritkatriel
Copy link
Member Author

A PR for the traceback display code is here: iritkatriel#31

Once this PR is merged I will make a PR from that branch into main. But in the meantime it can be reviewed there, diffed against this PR.

Lib/test/test_exception_group.py Outdated Show resolved Hide resolved
Lib/test/test_exception_group.py Outdated Show resolved Hide resolved
Comment on lines 79 to 81
self.assertIs(
type(MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])), MyEG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, if you want to override the way the traceback gets printed, subclassing EG isn't enough -- traceback-printing is (a) built into the C code, and (b) reimplemented in pure Python in traceback.py. To get something extra printed (e.g. err.__note__ if it exists) you will have to write your own traceback-printing code -- though there are various reusable bits and pieces in traceback.py.

Comment on lines 79 to 81
self.assertIs(
type(MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])), MyEG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, if you want to override the way the traceback gets printed, subclassing EG isn't enough -- traceback-printing is (a) built into the C code, and (b) reimplemented in pure Python in traceback.py. To get something extra printed (e.g. err.__note__ if it exists) you will have to write your own traceback-printing code and invoke it at the right point -- though there are various reusable bits and pieces in traceback.py.

Lib/test/test_exception_group.py Outdated Show resolved Hide resolved
Lib/test/test_exception_group.py Show resolved Hide resolved
Lib/test/test_exception_group.py Show resolved Hide resolved
Lib/test/test_exception_group.py Show resolved Hide resolved
Lib/test/test_exception_group.py Outdated Show resolved Hide resolved
Lib/test/test_exception_group.py Show resolved Hide resolved
@iritkatriel
Copy link
Member Author

(rebased)

@iritkatriel iritkatriel added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 22, 2021
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @iritkatriel for commit dbd72d1 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Oct 22, 2021
@iritkatriel iritkatriel merged commit f30ad65 into python:main Oct 22, 2021
@gvanrossum
Copy link
Member

“And so it begins…”

@1st1
Copy link
Member

1st1 commented Oct 23, 2021

@iritkatriel Congrats Irit. This is very impressive work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants