Textualize / rich

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

[REQUEST] fine-grained error locations #2609

Open flying-sheep opened 2 years ago

flying-sheep commented 2 years ago

How would you improve Rich?

Rich tracebacks should display Python 3.11’s fine-grained error locations.

PEP 657 defines codeobject.co_positions() to return an iterable yielding tuples containing (start_line, end_line, start_column, end_column). (note the _column entries are utf-8 byte offsets into the current line, not characters)

It looks like this

try:
    1 / 0
except Exception:
    _, _, tb = sys.exc_info()

instruction_positions = list(tb.tb_frame.f_code.co_positions())
# seems to be the line & byte offsets of the string “1 / 0” in above code:
assert instruction_positions[tb.tb_lasti] == (2, 2, 4, 9)

But there’s more information in there, e,g, Python 3.11’s whatsnew page displays a zero division error with a pointer distinguishing between the offending operator and the whole expression:

Traceback (most recent call last):
  File "calculation.py", line 54, in <module>
    result = (x / y / z) * (a / b / c)
              ~~~~~~^~~
ZeroDivisionError: division by zero

There might be inspiration for an implementation found in the many PRs listed in the tracking issue python/cpython#88116

What problem does it solve for you?

Rich should be able to use all information available. Since the release of Python 3.11, the stock traceback formatting might be more useful than rich’s output in some cases (rich displays locals, stock Python displays fine-grained error locations)

flying-sheep commented 4 months ago

Is rich.traceback still maintained? Between this and #1859, stock tracebacks just offer more info at this point.