This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Clarify chaining exceptions in tutorial/errors.rst
Type: Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Vladimir Ryabtsev, docs@python, eric.araujo, maxking, methane, miss-islington, rhettinger, willingc
Priority: normal Keywords: patch

Created on 2020-10-28 08:31 by Vladimir Ryabtsev, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23160 merged Vladimir Ryabtsev, 2020-11-05 02:57
PR 23162 merged methane, 2020-11-05 09:41
PR 23173 merged miss-islington, 2020-11-06 02:45
Messages (15)
msg379823 - (view) Author: Vladimir Ryabtsev (Vladimir Ryabtsev) * Date: 2020-10-28 08:31
A new section has been added to the page as a result of https://bugs.python.org/issue37826. The change: https://github.com/python/cpython/commit/dcfe111eb5602333135b8776996332a8dcf59392

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.
msg380394 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-11-05 03:55
Please note that tutorial is a tutorial. It is document to help new user who are learning Python.
Do you believe special attributes like __cause__ and __contexts__ are really worth to teach for tutorial readers?

Generally speaking, I think we should *reduce* some details from tutorial.
msg380396 - (view) Author: Vladimir Ryabtsev (Vladimir Ryabtsev) * Date: 2020-11-05 08:18
1. Such understanding of a tutorial is debatable. Tutorial is just a material for learning written with some system in mind, which is more interesting to read than dry reference material. A tutorial, generally dpeaking, may be both for beginners and for professionals.

2. The question about exception chaining is popular on Stackoverflow in people who came to Python with Java or C# background (see “python inner exception”).

3. Whatever material is given, it should not cause confusion, but now it does. Since this section has been added recently, it is better to fix it rather than remove entirely, aren’t you agree?
msg380397 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-11-05 08:51
> 1. Such understanding of a tutorial is debatable. Tutorial is just a material for learning written with some system in mind, which is more interesting to read than dry reference material. A tutorial, generally dpeaking, may be both for beginners and for professionals.

OK, I will send this topic to python-dev first.

> 2. The question about exception chaining is popular on Stackoverflow in people who came to Python with Java or C# background (see “python inner exception”).
> 3. Whatever material is given, it should not cause confusion, but now it does.

I searched it but I can not find confusion caused by this tutorial section. Please write a concrete URL caused by current tutorial?

> Since this section has been added recently, it is better to fix it rather than remove entirely, aren’t you agree?

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.
msg380425 - (view) Author: Vladimir Ryabtsev (Vladimir Ryabtsev) * Date: 2020-11-05 18:27
> I can not find confusion caused by this tutorial section

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

> Describing the default behavior and "from None" is enough for new users

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.
msg380432 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2020-11-05 20:16
I prefer the patch by Inada-san!

>> Describing the default behavior and "from None" is enough for new users
> Strange that you think that "from None" is more useful for beginners than these special attributes.

Doesn’t feel strange to me:  `raise Exc from exc` or `from None` shows how to use the mecanism, whereas the special attributes are about the implementation.  A tutorial can show how to use language features like for loops or with statements, but shouldn’t explain how to implement the protocols, that’s too much detail if you’re just starting to learn programming.
msg380434 - (view) Author: Vladimir Ryabtsev (Vladimir Ryabtsev) * Date: 2020-11-05 21:11
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__().
msg380435 - (view) Author: Vladimir Ryabtsev (Vladimir Ryabtsev) * Date: 2020-11-05 21:17
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.
msg380436 - (view) Author: Carol Willing (willingc) * (Python committer) Date: 2020-11-05 21:55
Thanks Vladimir for raising the issue, and Inada-san and Eric for following up on it.

I recommend the following:
- merge PR-23162 including its reference to builtin exceptions
- after merge of PR-23162, reworking PR-23160 to provide a brief note about __cause__ and __contex__ before the reference link to builtin exceptions

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.
msg380440 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-11-06 02:45
New changeset bde33e428d5b5f88ec7667598fd27d1091840537 by Inada Naoki in branch 'master':
bpo-42179: Doc/tutorial: Remove mention of __cause__ (GH-23162)
https://github.com/python/cpython/commit/bde33e428d5b5f88ec7667598fd27d1091840537
msg380441 - (view) Author: miss-islington (miss-islington) Date: 2020-11-06 03:06
New changeset e74fb2d7666eea43ad738528a565bb56bc88c28d by Miss Islington (bot) in branch '3.9':
bpo-42179: Doc/tutorial: Remove mention of __cause__ (GH-23162)
https://github.com/python/cpython/commit/e74fb2d7666eea43ad738528a565bb56bc88c28d
msg380570 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-11-09 01:45
>> 1. Such understanding of a tutorial is debatable. Tutorial is just a material for learning written with some system in mind, which is more interesting to read than dry reference material. A tutorial, generally dpeaking, may be both for beginners and for professionals.
>
> OK, I will send this topic to python-dev first.

For the record, there is a long thread in python-dev about this issue:

* main thread: https://mail.python.org/archives/list/python-dev@python.org/thread/MXMEFFYB6JXAKSS36SZ7DX4ASP6APWFP/
* another thread: https://mail.python.org/archives/list/python-dev@python.org/thread/WNHVZLEO3ZVDOFP2FHRBUQR4GY24RIJJ/

## High level discussion: focus on new user vs write more and more details.

More detail:
* Abdur-Rahmaan Janhangeer

Focus on new user:
* Paul Moore 
* Brett Cannon
* Guido van Rossum
* Kyle Stanley
* Carol Willing
* Serhiy Storchaka

## About this specific case. (Adding __context__ and __suppress_context vs removing __cause__)

Add __context__:
(no one)

Remove __cause__:
* Kyle Stanley
* Éric Araujo (in GH-23160)

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/ ).
msg380607 - (view) Author: Vladimir Ryabtsev (Vladimir Ryabtsev) * Date: 2020-11-09 18:50
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).
msg383114 - (view) Author: Carol Willing (willingc) * (Python committer) Date: 2020-12-16 02:47
New changeset 3f9fe23c05280dc5736c07bb0e968cdaf8c503d0 by Vladimir in branch 'master':
bpo-42179: Clarify exception chaining (GH-23160)
https://github.com/python/cpython/commit/3f9fe23c05280dc5736c07bb0e968cdaf8c503d0
msg384542 - (view) Author: Vladimir Ryabtsev (Vladimir Ryabtsev) * Date: 2021-01-06 23:37
The issue won't be fixed, but other useful changes applied.
History
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86345
2021-01-06 23:37:55Vladimir Ryabtsevsetstatus: open -> closed
resolution: wont fix
messages: + msg384542

stage: patch review -> resolved
2020-12-16 02:47:35willingcsetmessages: + msg383114
2020-11-09 18:50:29Vladimir Ryabtsevsetmessages: + msg380607
2020-11-09 01:45:35methanesetmessages: + msg380570
2020-11-06 03:06:07miss-islingtonsetmessages: + msg380441
2020-11-06 02:45:35miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request22086
2020-11-06 02:45:08methanesetmessages: + msg380440
2020-11-05 22:46:42zach.waresetfiles: - Cellular-Z 20200909 14:25:47 SLOT1.CSV
2020-11-05 22:40:26rednose42526qsetfiles: + Cellular-Z 20200909 14:25:47 SLOT1.CSV
2020-11-05 21:55:31willingcsetnosy: + willingc
messages: + msg380436
2020-11-05 21:17:26Vladimir Ryabtsevsetmessages: + msg380435
2020-11-05 21:11:08Vladimir Ryabtsevsetmessages: + msg380434
2020-11-05 20:16:06eric.araujosetnosy: + maxking, rhettinger, eric.araujo
messages: + msg380432
2020-11-05 18:27:57Vladimir Ryabtsevsetmessages: + msg380425
2020-11-05 09:41:13methanesetpull_requests: + pull_request22074
2020-11-05 08:51:11methanesetmessages: + msg380397
2020-11-05 08:18:39Vladimir Ryabtsevsetmessages: + msg380396
2020-11-05 03:55:21methanesetnosy: + methane
messages: + msg380394
2020-11-05 02:57:31Vladimir Ryabtsevsetkeywords: + patch
stage: patch review
pull_requests: + pull_request22072
2020-10-28 08:31:31Vladimir Ryabtsevcreate