Skip to content

gh-99553: add tests for ExceptionGroup wrapping #99615

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

Merged
merged 6 commits into from
Apr 11, 2023

Conversation

Zac-HD
Copy link
Contributor

@Zac-HD Zac-HD commented Nov 20, 2022

Follows #99572 (comment) to clarify a potential point of confusion. CC @iritkatriel (thank you again!)

@Zac-HD Zac-HD requested a review from iritkatriel as a code owner November 20, 2022 08:13
@bedevere-bot bedevere-bot added awaiting review tests Tests in the Lib/test dir labels Nov 20, 2022
@Zac-HD Zac-HD force-pushed the zac/custom-exceptiongroup-docs-tests branch from 8158778 to 4b9e01a Compare November 20, 2022 08:26
@sobolevn
Copy link
Member

@Zac-HD I cannot do that, but if you change you PR title from gh-99572 to gh-99553 - the CI failure will go away :)

@AlexWaygood AlexWaygood changed the title gh-99572: clarify doc and add tests gh-99553: clarify doc and add tests Nov 20, 2022
@hauntsaninja hauntsaninja reopened this Nov 20, 2022
A subclass of :exc:`BaseExceptionGroup` that extends :exc:`Exception`
cannot wrap :exc:`BaseException`\ s, as is the case with :exc:`ExceptionGroup`.
However, there is no further restriction: a :exc:`BaseExceptionGroup` subclass
which extends :exc:`KeyError` could wrap any :exc:`Exception`.
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a strange thing to say in a doc, because why would you expect it to not wrap any Exception?

Maybe instead of the second sentence we could just explain the motivation for the restriction: “This ensures that wrapped BaseExceptions are not caught by ‘except Exception’.”

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why would you expect it to not wrap any Exception?

Suppose we have class MyEG(BaseExceptionGroup, X):, where except X: will catch such a group because it extends X. We disallow wrapping BaseExceptions in an ExceptionGroup because they would not be caught by except Exception: if unwrapped.

However this logic is not applied consistently: if X is e.g. KeyError, then except KeyError: can indeed catch MyEG("", [ValueError()]). I find this inconsistency surprising, which is why I commented on #99572 and then opened this PR!

(I'm not trying to reopen the design discussion, just trying to explain my perspective)

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I see. Well yes, the Exception case is different - people use ‘expect Exception’ as ‘catch everything except fatal errors’.

@AlexWaygood AlexWaygood changed the title gh-99553: clarify doc and add tests gh-99553: ExceptionGroups: clarify doc and add tests Nov 20, 2022
@Zac-HD
Copy link
Contributor Author

Zac-HD commented Nov 29, 2022

Ready to merge, I think?

Comment on lines 968 to 971
A subclass of :exc:`BaseExceptionGroup` that extends :exc:`Exception`
cannot wrap :exc:`BaseException`\ s, as is the case with :exc:`ExceptionGroup`.
However, there is no further restriction: a :exc:`BaseExceptionGroup` subclass
which extends :exc:`KeyError` could wrap any :exc:`Exception`.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this comment is easy to understand without the justification. How about this?

Suggested change
A subclass of :exc:`BaseExceptionGroup` that extends :exc:`Exception`
cannot wrap :exc:`BaseException`\ s, as is the case with :exc:`ExceptionGroup`.
However, there is no further restriction: a :exc:`BaseExceptionGroup` subclass
which extends :exc:`KeyError` could wrap any :exc:`Exception`.
Like :exc:`ExceptionGroup`, user-defined subclasses of :exc:`BaseExceptionGroup`
that extend :exc:`Exception` can only wrap :exc:`Exception` subclasses.
This is to prevent exceptions that represent fatal errors from being wrapped in an
exception group which can be caught by ``except Exception``. See also :ref:`exceptions`.

Copy link
Contributor

@kumaraditya303 kumaraditya303 left a comment

Choose a reason for hiding this comment

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

Please address Irit's review.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Zac-HD
Copy link
Contributor Author

Zac-HD commented Apr 11, 2023

I've taken a few attempts, and haven't found a way to explain this which reliably clarifies rather than confuses, so I'll just close the PR 🙂

@Zac-HD Zac-HD closed this Apr 11, 2023
@hauntsaninja
Copy link
Contributor

hauntsaninja commented Apr 11, 2023

If it's hard to figure out appropriate changes to the docs, I can at least merge the tests.

@hauntsaninja hauntsaninja reopened this Apr 11, 2023
@hauntsaninja hauntsaninja added the needs backport to 3.11 only security fixes label Apr 11, 2023
@hauntsaninja hauntsaninja changed the title gh-99553: ExceptionGroups: clarify doc and add tests gh-99553: add tests for ExceptionGroup wrapping Apr 11, 2023
@hauntsaninja hauntsaninja merged commit 4cd1cc8 into python:main Apr 11, 2023
@miss-islington
Copy link
Contributor

Thanks @Zac-HD for the PR, and @hauntsaninja for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 11, 2023
(cherry picked from commit 4cd1cc8)

Co-authored-by: Zac Hatfield-Dodds <[email protected]>
@bedevere-bot
Copy link

GH-103435 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Apr 11, 2023
@hauntsaninja
Copy link
Contributor

Thank you!

miss-islington added a commit that referenced this pull request Apr 11, 2023
(cherry picked from commit 4cd1cc8)

Co-authored-by: Zac Hatfield-Dodds <[email protected]>
@Zac-HD Zac-HD deleted the zac/custom-exceptiongroup-docs-tests branch April 11, 2023 07:58
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
aisk pushed a commit to aisk/cpython that referenced this pull request Apr 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants