fastapi / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.
https://typer.tiangolo.com/
MIT License
15.79k stars 672 forks source link

Add support for "Exception.add_note(...)" (PEP 678) #505

Open libexpand opened 1 year ago

libexpand commented 1 year ago

First Check

Commit to Help

Example Code

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

Description

The note does not appear when pretty_exceptions_enable=True PEP 678 – Enriching Exceptions with Notes

Wanted Solution

I would like the note to appear when pretty exceptions are enabled.

Wanted Code

try:
    raise TypeError('bad type')
except Exception as e:
    e.add_note('Add some information')
    raise

Alternatives

No response

Operating System

Linux, Windows

Operating System Details

No response

Typer Version

0.7.0

Python Version

Python 3.11.0

Additional Context

No response

ESPR3SS0 commented 6 months ago

The notes are only omitted when rich is installed. When rich is not installed the notes are included.

So it looks like the notes are omitted by rich.Console in the function 'print_exception', and therefore not something typer could directly fix.

As a work-around Typer could manually print the notes until rich.Console implements it. I wouldn't mind looking into this if this behavior is desired.

peterjc commented 3 weeks ago

This was logged with Rich as https://github.com/Textualize/rich/issues/2652