Display exception notes in tracebacks #14039
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PEP 678 introduced the ability to add notes to exception objects. This has been released in Python 3.11 and is currently not implemented in IPython. These changes are fully compatible with older Python versions that don't include PEP 678.
Here's a sample test that shows the consistency in Python's stdlib traceback module (test 1) and the difference between Python and IPython's runtimes (test 2):
When executed with Python 3.11, both notes are displayed in both tracebacks:
In IPython's VerboseTB does not yet handle exception notes:
The changes I am suggesting are inspired from implementation of Lib/traceback.py (search for
__notes__
) and improvements for dealing with edge cases more nicely in cpython#103897.Although notes are meant to be strings only, I kept some inspiration from the existing exception handling to ensure that the notes are uncolored and bytes decoded, if there are any. I am definitely open to using a different color if deemed better. For context,
bpython
keeps the notes uncolored, and Python's tutorial puts them in light gray, like the line numbers.Here's how the test 2 looks like after these changes:

🐍 🤹♂️