Skip to content

Proposal: highlight exact operation in tracebacks #11827

@alexmojaki

Description

@alexmojaki

Hi! I recently created executing, a library which can identify the AST node being executed by a frame in many cases. I'd like to use this to improve Python tracebacks in many places, starting with IPython. It's always disappointed me that Python only points to a line in each frame in tracebacks, especially when the expression/statement at fault spans many lines. Sometimes it can be really hard to interpret.

This toy library offers a nice example of what tracebacks using executing can look like:

heartrate

Basically, highlighting the text background could make it clear what's being executed without interfering with other coloring and formatting such as syntax highlighting.

I tried to jump to coding this straight away but quickly saw that it's quite complicated. The introspection magic stuff is already done, it's just a matter of updating the formatting in ultratb, but that still looks quite hard, especially as I'm new to this code. So I'm creating this issue first to check if the idea is acceptable before wasting my time.

I'm expecting two main concerns: reliability and dependencies.

executing is extremely reliable. By that I mean that I'm confident it will never identify the wrong node. If the linked explanation doesn't convince you, I encourage you to try finding a case where it goes wrong. If it helps, I'm also using executing in two other libraries of mine: sorcery and snoop. However executing often can't identify the node at all, so not all frames will have something highlighted. Over time this situation will improve as I implement more operations.

Two new dependencies would need to be added: executing and asttokens (to convert AST nodes to source code positions, I don't know any alternative). The only transitive dependency from these is six. In my experience asttokens is an excellent library with active and responsive contributors.

Does this sound good to you?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions