Skip to content

Commit

Permalink
PEP 678: more rejected ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Dec 27, 2021
1 parent b072483 commit 9a118c2
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pep-0678.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ exception chaining, and are unnecessary with ``BaseException.__note__`` :
Explanation: # Hence this PEP!
You can reproduce this error by ...
**Where these factors are not problematic, we encourage use of exception chaining
rather than** ``__note__`` .


Subclass Exception and add ``__note__`` downstream
--------------------------------------------------
Expand All @@ -229,6 +232,49 @@ proposed ``__note__`` semantics, but this would be rarely and inconsistently
applicable.


Augment the ``raise`` statement
-------------------------------
One discussion proposed ``raise Exception() with "note contents"`` .
This would be a strictly additional feature over adding the attribute and modifying
the traceback-formatting code, and we do not expect ``__note__`` to be so
frequently-used as to justify new syntax.


Structured API rather than string attribute
-------------------------------------------
Instead of a string-or-None, ``__note__`` could be a new object with a structured API
and a ``__str__()`` method for printing at trackback time.
We have not identified any scenario where libraries would want to do anything but either
concatenate or replace notes, and so the additional complexity does not seem justified.

Permitting ``__note__`` to be "None or any object with a ``__str__()`` method" would
allow for experimentation with such proposals, and forward-compatibility one was adopted.
We see this as a flexibility/usability tradeoff, and in the absence of any concrete
use-case we narrowly prefer to guarantee that notes are strings but remain open to
revisiting the decision.


Add a helper function ``contexlib.add_exc_note()``
--------------------------------------------------
We don't expect notes to be used frequently enough to justify this either, but
provide the following recipe::

@contextlib.contextmanager
def add_exc_note(
note: str,
exc: type[BaseException] | tuple[type[BaseException], ...] = BaseException,
concatenate: str | None = "\n\n",
):
try:
yield
except exc as err:
if concatenate is None or err.__note__ is None:
err.__note__ = note
else:
err.__note__ = err.__note__ + concatenate + note
raise


Store notes in ``ExceptionGroup`` s
-----------------------------------
Initial discussions proposed making a more focussed change by thinking about how to
Expand Down

0 comments on commit 9a118c2

Please sign in to comment.