Skip to content

Commit f373d8b

Browse files
Zac-HDiritkatriel
andauthored
PEP 678: Clarify how the notes tuple is updated and when it is copied (#2377)
Co-authored-by: Irit Katriel <[email protected]>
1 parent 358854a commit f373d8b

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

pep-0678.rst

+18-13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ therefore propose to add:
5959

6060
Example usage
6161
-------------
62+
::
6263

6364
>>> try:
6465
... raise TypeError('bad type')
@@ -140,10 +141,11 @@ are collecting multiple exception objects to handle together. [1]_
140141
Specification
141142
=============
142143

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.
147149

148150
``del err.__notes__`` clears the contents of the ``__notes__`` attribute,
149151
leaving it an empty tuple as if ``.add_note()`` had never been called. This
@@ -198,6 +200,8 @@ implements ``.add_note()`` and ``__notes__``.
198200
Rejected Ideas
199201
==============
200202

203+
.. _print_idea:
204+
201205
Use ``print()`` (or ``logging``, etc.)
202206
--------------------------------------
203207
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
319323
applicable.
320324

321325

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.
331336

332337

333338

0 commit comments

Comments
 (0)