rich icon indicating copy to clipboard operation
rich copied to clipboard

Add support for "Exception.add_note(...)" (PEP 678)[REQUEST]

Open libexpand opened this issue 3 years ago • 4 comments

The note does not appear when console.print_exception(show_locals=True) PEP 678 – Enriching Exceptions with Notes Should be:

>>> try:
...     raise TypeError('bad type')
... except Exception as e:
...     e.add_note('Add some information')
...     raise
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: bad type
Add some information
>>>

libexpand avatar Nov 17 '22 00:11 libexpand

#1859 also kind of track this

Bludator avatar Mar 30 '25 01:03 Bludator

We would very much like support for exception notes, only just now realized they weren't working for our users.

ofek avatar Apr 20 '25 15:04 ofek

This issue can be closed. add_note is supported since https://github.com/Textualize/rich/commit/f19b246826d23ec14ee09edeedc5953c640792ed .

from master

python foo.py 
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ rich/foo.py:5 in <module>             │
│                                                                                                  │
│   2 console = Console()                                                                          │
│   3                                                                                              │
│   4 try:                                                                                         │
│ ❱ 5 │   raise ValueError("This is a test error")                                                 │
│   6 except ValueError as e:                                                                      │
│   7 │   e.add_note("note")                                                                       │
│   8 │   console.print_exception(show_locals=True)                                                │
│                                                                                                  │
│ ╭────────────────────── locals ───────────────────────╮                                          │
│ │ console = <console width=166 ColorSystem.TRUECOLOR> │                                          │
│ │       e = ValueError('This is a test error')        │                                          │
│ ╰─────────────────────────────────────────────────────╯                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
ValueError: This is a test error
[NOTE] note

using 13.9.4 the NOTE isn't shown on the bottom

repro

from rich.console import Console
console = Console()

try:
    raise ValueError("This is a test error")
except ValueError as e:
    e.add_note("note")
    console.print_exception(show_locals=True)

Hnasar avatar Apr 22 '25 22:04 Hnasar

I can confirm that this now works in the most recent release, thanks!

ofek avatar Apr 22 '25 22:04 ofek