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.1k stars 1.69k forks source link

2286 pep657 error code ranges in traceback #3389

Open Gothingbop opened 2 weeks ago

Gothingbop commented 2 weeks ago

Type of changes

Checklist

Description

Pull request for https://github.com/Textualize/rich/issues/2286

Replicates some of the functionality in PEP 657 by extracting the col range of the exception and adds the style to the Syntax using Syntax.stylize_range().

Effectively repurposes code found in some private functions in the base traceback module: _get_code_position() and _walk_tb_with_full_positions(). The code would be cleaner if those functions were not private or we were OK with using private functions (raises an error when running the mypy type checking).

I can also abstract the loop into a generator to more closely replicate the _walk_tb_with_full_positions() functionality, but I'm unsure where the best spot for that would be.

Base Python traceback

image

Rich traceback before

image

Rich traceback with highlighing

image

willmcgugan commented 1 day ago

Great in principle.

I think we can improve on the styles a bit. And some of the information present in the ascii version doesn't seem to be styled.

This will also need to handle older Pythons.