Skip to content
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

PEP 678: Clarify how the notes tuple is updated and when it is copied #2377

Merged
merged 6 commits into from
Mar 3, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions pep-0678.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ therefore propose to add:

Example usage
-------------
::

>>> try:
... raise TypeError('bad type')
Expand Down Expand Up @@ -140,10 +141,10 @@ are collecting multiple exception objects to handle together. [1]_
Specification
=============

``BaseException`` gains a new read-only attribute ``__notes__``, an initially
empty tuple, and a new method ``.add_note(note: str)``. ``note`` is added to
the exception's notes, which appear in the standard traceback after the
exception string. A ``TypeError`` is raised if ``note`` is not a string.
``BaseException`` gains a new new method ``.add_note(note: str)``. Notes are
exposed as a tuple via the read-only attribute ``__notes__``, and appear in
the standard traceback after the exception string. ``.add_note()`` raises
``TypeError`` if ``note`` is not a string.
Zac-HD marked this conversation as resolved.
Show resolved Hide resolved

``del err.__notes__`` clears the contents of the ``__notes__`` attribute,
leaving it an empty tuple as if ``.add_note()`` had never been called. This
Expand Down Expand Up @@ -198,6 +199,8 @@ implements ``.add_note()`` and ``__notes__``.
Rejected Ideas
==============

.. _print_idea:

Use ``print()`` (or ``logging``, etc.)
--------------------------------------
Reporting explanatory or contextual information about an error by printing or
Expand Down Expand Up @@ -319,15 +322,16 @@ proposed ``__notes__`` semantics, but this would be rarely and inconsistently
applicable.


Store notes in ``ExceptionGroup``\ s
------------------------------------
Initial discussions proposed making a more focussed change by thinking about
how to associate messages with the nested exceptions in ``ExceptionGroup`` s,
such as a list of notes or mapping of exceptions to notes. However, this would
force a remarkably awkward API and retains a lesser form of the
cross-referencing problem discussed under "use ``print()``" above; if this PEP
is rejected we prefer the status quo. Finally, of course, ``__notes__`` are
not only useful with ``ExceptionGroup``\ s!
Don't attach notes to ``Exception``\ s, just store them in ``ExceptionGroup``\ s
--------------------------------------------------------------------------------
The initial motivation for this PEP was to associate a note with each error
in an ``ExceptionGroup``. At the cost of a remarkably awkward API and the
cross-referencing problem discussed `above <print_idea>`__, this
use-case could be supported by storing notes on the ``ExceptionGroup``
instance instead of on each exception it contains.

We believe that the cleaner interface, and other use-cases described above,
are sufficient to justify the more general feature proposed by this PEP.



Expand Down