Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
48.13k stars 1.69k forks source link

Enable rendering of rich exception objects in traceback #3325

Open pradyunsg opened 2 months ago

pradyunsg commented 2 months ago

Type of changes

Checklist

Description

See https://github.com/Textualize/rich/discussions/2717 for discussion. This enables exceptions to be rich objects and be rendered via the rich rendering pipeline, in tracebacks. This can be tested manually by running python -m rich.traceback whose example now uses a rich object instead of the NameError.

pradyunsg commented 2 months ago

This is currently a draft since I'm not sure how exactly exceptions within the rich object rendering should be handled -- I'm leaning toward trying to catch them and presenting a fallback like "render failed" or similar. Not quite sure how to do that, so figured I'd open this as a draft PR in case y'all have any opinions/suggestions on how to achieve that.

pradyunsg commented 2 months ago

OK, I think I figured out something that works! 🎉

Using the Console object to eagerly render the exception and iterating through to collect all the segments, before yielding those segments forward. This enables this logic to catch any rendering-related issues inline, rather than needing to handle them at some other level (eg: console.render).

Marking this as ready for review, since I think everything that is necessary to land this is in place now[^1] -- code, tests, documentation, changelog, contributing.md. :)

[^1]: Assuming of course that there are no concerns with the implementation/approach. 😉

pradyunsg commented 2 months ago

Oh, and here's an SVG showing that this works!

exception