@@ -59,6 +59,7 @@ therefore propose to add:
59
59
60
60
Example usage
61
61
-------------
62
+ ::
62
63
63
64
>>> try:
64
65
... raise TypeError('bad type')
@@ -140,10 +141,11 @@ are collecting multiple exception objects to handle together. [1]_
140
141
Specification
141
142
=============
142
143
143
- ``BaseException `` gains a new read-only attribute ``__notes__ ``, an initially
144
- empty tuple, and a new method ``.add_note(note: str) ``. ``note `` is added to
145
- the exception's notes, which appear in the standard traceback after the
146
- exception string. A ``TypeError `` is raised if ``note `` is not a string.
144
+ ``BaseException `` gains a new new method ``.add_note(note: str) ``. Notes are
145
+ exposed as a tuple via the read-only attribute ``__notes__ ``, and appear in
146
+ the standard traceback after the exception string. ``.add_note(note) `` replaces
147
+ ``__notes__ `` with a new tuple equal to ``__notes__ + (note,) ``, if ``note ``
148
+ is a string, and raises ``TypeError `` otherwise.
147
149
148
150
``del err.__notes__ `` clears the contents of the ``__notes__ `` attribute,
149
151
leaving it an empty tuple as if ``.add_note() `` had never been called. This
@@ -198,6 +200,8 @@ implements ``.add_note()`` and ``__notes__``.
198
200
Rejected Ideas
199
201
==============
200
202
203
+ .. _print_idea :
204
+
201
205
Use ``print() `` (or ``logging ``, etc.)
202
206
--------------------------------------
203
207
Reporting explanatory or contextual information about an error by printing or
@@ -319,15 +323,16 @@ proposed ``__notes__`` semantics, but this would be rarely and inconsistently
319
323
applicable.
320
324
321
325
322
- Store notes in ``ExceptionGroup ``\ s
323
- ------------------------------------
324
- Initial discussions proposed making a more focussed change by thinking about
325
- how to associate messages with the nested exceptions in ``ExceptionGroup `` s,
326
- such as a list of notes or mapping of exceptions to notes. However, this would
327
- force a remarkably awkward API and retains a lesser form of the
328
- cross-referencing problem discussed under "use ``print() ``" above; if this PEP
329
- is rejected we prefer the status quo. Finally, of course, ``__notes__ `` are
330
- not only useful with ``ExceptionGroup ``\ s!
326
+ Don't attach notes to ``Exception ``\ s, just store them in ``ExceptionGroup ``\ s
327
+ --------------------------------------------------------------------------------
328
+ The initial motivation for this PEP was to associate a note with each error
329
+ in an ``ExceptionGroup ``. At the cost of a remarkably awkward API and the
330
+ cross-referencing problem discussed `above <print_idea >`__, this
331
+ use-case could be supported by storing notes on the ``ExceptionGroup ``
332
+ instance instead of on each exception it contains.
333
+
334
+ We believe that the cleaner interface, and other use-cases described above,
335
+ are sufficient to justify the more general feature proposed by this PEP.
331
336
332
337
333
338
0 commit comments