question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[REQUEST] PEP657 error code ranges in tracebacks

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pradyunsgcommented, May 20, 2022

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

1reaction
tonybaloneycommented, May 20, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

PEP 657 – Include Fine Grained Error Locations in Tracebacks
In order to identify the range of source code being executed when exceptions are raised, this proposal requires adding new data for every ......
Read more >
HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Read more >
Python 3.11: Cool New Features for You to Try - Real Python
More Informative Error Tracebacks; Faster Code Execution; Nicer Syntax for ... python inverse.py Traceback (most recent call last): File ...
Read more >
What's New In Python 3.11 — Python 3.9.9 documentation
Traceback (most recent call last): File "distance.py", line 11, ... sqlite3 exceptions now include the SQLite extended error code as sqlite_errorcode and ...
Read more >
What's New In Python 3.11-BeJSON.com
Traceback (most recent call last): File "query.py", line 37, in <module> ... extended PEP 657 position information (end line number, column and end...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found