cknd / stackprinter

Debugging-friendly exceptions for Python
MIT License
1.28k stars 37 forks source link

Output for ExceptionGroups hides sub-exceptions #65

Closed ugtar closed 11 months ago

ugtar commented 11 months ago

Only the outer-most exception seems be be visible in the output for a handled ExceptionGroup. here is a contrived example:

>>> def fn():
...     e = ExceptionGroup("multiple exceptions",
...             [ExceptionGroup("file not found",
...                 [FileNotFoundError("unknown filename file1.txt"), 
...                  FileNotFoundError("unknown filename file2.txt")]), 
...              KeyError("missing key")])
...     raise e

the normal output shows the sub-exceptions:

>>> fn()
  + Exception Group Traceback (most recent call last):
  |   File "<stdin>", line 1, in <module>
  |   File "<stdin>", line 7, in fn
  | ExceptionGroup: multiple exceptions (2 sub-exceptions)
  +-+---------------- 1 ----------------
    | ExceptionGroup: file not found (2 sub-exceptions)
    +-+---------------- 1 ----------------
      | FileNotFoundError: unknown filename file1.txt
      +---------------- 2 ----------------
      | FileNotFoundError: unknown filename file2.txt
      +------------------------------------
    +---------------- 2 ----------------
    | KeyError: 'missing key'
    +------------------------------------

but with stackprinter

>>> import stackprinter
>>> stackprinter.set_excepthook()
>>> fn()
File "<stdin>", line 1, in <module>

File "<stdin>", line 7, in fn

ExceptionGroup: multiple exceptions (2 sub-exceptions)

all we see is there are sub-exceptions.

I don't have any great suggestions, but maybe at least stackprinter could fall back to the basic output in the case of an ExceptionGroup

cknd commented 11 months ago

Ah dang. Agreed, it should at least fall back to default output, if not fully supporting exception groups

ugtar commented 11 months ago

I thought to work around this by setting add_summary=True always, but it looks like the summary traceback is not the builtin traceback format, but a similar one, and in the case of ExceptionGroups it's still no good

>>> fn()
File "<stdin>", line 1, in <module>

File "<stdin>", line 7, in fn

---- (full traceback above) ----
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in fn

ExceptionGroup: multiple exceptions (2 sub-exceptions)

:man_shrugging:

maybe the summary could be made to use the default formatter?

ugtar commented 11 months ago

Also, fwiw, falling back to the native exception formatter is what ipython is currently doing for exception groups https://github.com/ipython/ipython/pull/14108/

cknd commented 11 months ago

Fixed in the latest 0.2.11 on pypi! Thanks for the report