Skip to content

[REQUEST] fine-grained error locations #2609

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

Open
flying-sheep opened this issue Oct 25, 2022 · 1 comment
Open

[REQUEST] fine-grained error locations #2609

flying-sheep opened this issue Oct 25, 2022 · 1 comment

Comments

@flying-sheep
Copy link

flying-sheep commented Oct 25, 2022

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

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

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

No branches or pull requests

1 participant