Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable rendering of rich exception objects in traceback #3325

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

pradyunsg
Copy link

@pradyunsg pradyunsg commented Apr 7, 2024

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

See #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 pradyunsg marked this pull request as draft April 7, 2024 19:32
@pradyunsg
Copy link
Author

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
Copy link
Author

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 now1 -- code, tests, documentation, changelog, contributing.md. :)

Footnotes

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

@pradyunsg pradyunsg marked this pull request as ready for review April 7, 2024 20:01
@pradyunsg pradyunsg force-pushed the rich-exceptions-in-traceback branch from 9e34085 to 9cb8a34 Compare April 7, 2024 20:04
@@ -722,7 +739,11 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]:
from .console import Console

console = Console()
import sys
Copy link
Author

@pradyunsg pradyunsg Apr 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys was unused at this point, and there's already a top-level import sys.

@pradyunsg
Copy link
Author

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

exception

except Exception:
console.print_exception()
result = console.file.getvalue()
print(result)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These prints are useful for diagnosing failures, and pytest will capture this output appropriately. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant