rich icon indicating copy to clipboard operation
rich copied to clipboard

[REQUEST] PEP657 error code ranges in tracebacks

Open tonybaloney opened this issue 3 years ago • 7 comments

Python 3.11 implements code position ranges in tracebacks.

from rich.traceback import install
install(show_locals=True)
data = [
    {
        "person": {
            "friends": [
                {"name": "Sally",
                "age": 32},
                {"name": "Yuri",
                "age": 43}
            ],
            "location": "Warsaw, Poland"
        }
    }
]

print(data[0]["person"]["friends"][2]["name"])

The default exception handler will print the position of the offending error in the traceback:

Traceback (most recent call last):
  File "/Users/anthonyshaw/projects/python-3.11-demos/error_messages.py", line 15, in <module>
    print(data[0]["person"]["friends"][2]["name"])
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

Rich does not use this data (but still looks very pretty) screenshot 2022-05-20 at 14 46 09

IT would be great to get the trace messages with arrows.

tonybaloney avatar May 20 '22 04:05 tonybaloney

Note: TracebackException now has the offset and end_offset attributes which expose the information needed to print the arrows.

https://github.com/python/cpython/blob/3.11/Lib/traceback.py#L852-L864

tonybaloney avatar May 20 '22 04:05 tonybaloney

This is very much a drive-by comment: I wonder if there's Unicode characters that could be used for this.

pradyunsg avatar May 20 '22 06:05 pradyunsg

I'd love to implement this. Not certain how to render it yet. Rich shows a few lines before and after the highlighted line, and I wouldn't want to break that up with the ~~~~~~~^^^ characters.

But there are other ways we could emphasize those error columns, with underline or reverse video perhaps. We can experiment a little.

@darrenburns @DrBenton Any suggestions?

willmcgugan avatar May 20 '22 10:05 willmcgugan

Inline emphasis looks to be a good way to manage this indeed, at least for a first iteration :slightly_smiling_face:

olivierphi avatar May 20 '22 15:05 olivierphi

Note: TracebackException now has the offset and end_offset attributes which expose the information needed to print the arrows. https://github.com/python/cpython/blob/3.11/Lib/traceback.py#L852-L864

It seems that the offset and end_offset attributes are set only for SyntaxErrors, if I'm not wrong?

It seems that FrameSummary instances, generated via StackSummary.extract(walk_tb(traceback)) , now have a colno and end_colno attributes, which I guess we could potentially use to report the exact location of the error on the line. However, in my few tests these 2 attributes' values seem to always be None? :thinking:

olivierphi avatar May 24 '22 10:05 olivierphi

For highlighting, may I suggest something similar to what I have shown in https://github.com/Textualize/rich/issues/1829?

aroberge avatar May 24 '22 11:05 aroberge

We have the ability to highlight additional ranges within Syntax now. So this might be a good point to tackle this.

willmcgugan avatar Jul 13 '22 14:07 willmcgugan