-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Clarify chaining exceptions in tutorial/errors.rst #86345
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
Comments
A new section has been added to the page as a result of https://bugs.python.org/issue37826. The change: dcfe111 The wording it uses (in the beginning of section 8.5), defines chaining as setting __cause__ attribute in an exception, and later states that "Exception chaining happens automatically when an exception is raised inside an exception handler or finally section". This may lead the reader to a wrong idea that re-raising an exception without "from" in "except" and "finally" automatically sets __cause__. In reality it sets only __context__ attribute, which is similar concept to __cause__, but work a bit differently, as explained in library/exceptions.rst. I suggest to mention that difference and provide a link to the main article. |
Please note that tutorial is a tutorial. It is document to help new user who are learning Python. Generally speaking, I think we should *reduce* some details from tutorial. |
|
OK, I will send this topic to python-dev first.
I searched it but I can not find confusion caused by this tutorial section. Please write a concrete URL caused by current tutorial?
I prefer removing mention to __cause__, instead of adding mention to __context__. No need to remove entire section. We can introduce high level overview of context chaining. Describing the default behavior and "from None" is enough for new users. |
Inada, have you read the very first message in this ticket? It explains why this wording may cause confusion (and it did in me), and describes the problem part. A link for your convenience: https://docs.python.org/3/tutorial/errors.html#exception-chaining
Strange that you think that "from None" is more useful for beginners than these special attributes. Without understanding of __cause__ and __context__, stack traceback message looks like magic. Say you want to handle an exception and retrieve its cause (context) in runtime (this is what exception chaining for) – this section makes no clues about how to do that. |
I prefer the patch by Inada-san!
Doesn’t feel strange to me: |
We have automatic chaining, so you don't need to use "from X" unless you want to have some control on the traceback message. Even without knowing of this syntax (and without using "from exc"), a user will get a traceback message similar to what is shown in the example. What is the purpose of the entire section then? As I see it, the purpose might be providing some details about how exactly chaining works, so a user: a) could make an informed decision whether they need "from X" or not, b) would know how to retrieve the linked exception programmatically. I generally feel that we don't want to deprive a user from special attributes, in Python they are everywhere, you cannot even construct a class instance without __init__(). |
Also, the choice of the exception type in the example looks not very apt: you raise "IOError" but the traceback message says "OSError" (which is due to strange design decision "IOError = OSError"). For the tutorial, I would choose an exception that does not disguise as another exception. |
Thanks Vladimir for raising the issue, and Inada-san and Eric for following up on it. I recommend the following:
This would provide a clear tutorial example for the majority of users. For the fraction of users, like Vladimir, a sentence as part of the reference link could address a bit more about __cause__ and __context__ without confusing folks. |
New changeset bde33e4 by Inada Naoki in branch 'master': |
New changeset e74fb2d by Miss Islington (bot) in branch '3.9': |
For the record, there is a long thread in python-dev about this issue:
## High level discussion: focus on new user vs write more and more details. More detail:
Focus on new user:
## About this specific case. (Adding __context__ and __suppress_context vs removing __cause__) Add __context__: Remove __cause__:
Riccardo Polignieri asked that to be very careful about removing something, but he did not vote for adding __context__ and __supress_context__. -- I merged PR-23162 for keep focus on new users and consistent for now. But I have not closed this issue yet because documentation WG may revisit the issue. (see https://mail.python.org/archives/list/python-dev@python.org/message/IWW2YBLJK4T3OWSKDUDVDVXPWDGIFWTC/ ). |
All right, you won. I hope beginner users will be happy :) I removed my proposal paragraph about __cause__ and __context__ and kept only changes about exception type (https://bugs.python.org/issue42179#msg380435). |
The issue won't be fixed, but other useful changes applied. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: