-
-
Notifications
You must be signed in to change notification settings - Fork 204
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
Multiline statements/expressions and context #92
Comments
For the terminal I think it is a tough decision, and one of the reason I want to separate generating a data structure that contain the relevant information and the rendering is that in Jupyter Notebook, for example, we use HTML to have 1) collapsible elements and 2) values as a tooltip on hover. I find having the context also quite more powerful than the value themselves; but not only for multiline statements; but to understand what the author of the code meant w/o having to actually go open the file. |
Hello! I wanted to leave a +1 for multiple lines of code context & for variables set apart from code -- what motivated me to write stackprinter despite knowing better-exception was that I liked IPython's multiline code contexts, I just wanted to see all variables from that context too. But I found it hard to skim/recognize code at a glance when the source lines were interleaved with variable values, especially if the values themselves are multi-line, like prettyprinted dicts or numpy arrays. So I felt the optimum (for terminals) is after all to separate the two. Colors may help tie them back together a bit, but in practice it still seems to work well even in plaintext (The main usability secret might be to only show the variables that occur in the visible source context, I think. At least by default) @Carreau Something I had long planned was to move this frame inspection module into its own package, so arbitrary formatters could sit on top (both terminals or HTML for notebooks). Would that be interesting at all? The module takes a frame and returns a tokenized map of the code (that knows where which variable occurs), together with the variable values (plus some minor niceties like dot lookup). That allows to rerender the original source character-by-character, but with arbitrary annotations on the variable names. It currently feeds into the terminal color thing in the screenshot above, but of course once you have an annotated version of the source it's also straightforward to e.g. generate a source listing in the browser where names in the code can be clicked / hovered to see their values, etc. I'd be very happy if anyone could put source-code-with-debug-values into jupyter, I just wanted to offer this since I have it sitting around & would be happy to help push it along. |
@Carreau I've started work on a library https://github.com/alexmojaki/stack_data which focuses just on the data aspect of tracebacks. I'm currently integrating it into stackprinter in cknd/stackprinter#23 because it's an easier place to get started. IPython/Jupyter will be next, followed by better-exceptions if possible. |
I find it annoying when tracebacks only show a single line for a frame, when that frame may be executing a multiline expression or frame and that single line leaves out critical context. I see that better_exceptions doesn't address this. Here's an example:
Output:
In particular this doesn't show the function
div
or the argumentb
(which causes the exception) at the top. It seems that multiline function calls generally show the last line containing significant tokens (i.e. not the trailing)
) which is often not going to be what the user wants.There was a discussion about solving this and showing all the lines of a multiline statement in the traceback: https://bugs.python.org/issue12458 It doesn't look like that's going to happen anytime soon, but a change has been merged which shows the first line instead of the last, so in the example above it shows
div(
in Python 3.8 (I just tested it).Some tools such as IPython (https://github.com/Qix-/better-exceptions/issues/10#issuecomment-513497696) and Sentry show a few lines of context before and after the line that the traceback points to. This context may be useful in its own right and some users may want something like that. But I don't know how useful it is and it makes tracebacks significantly longer. I don't think that should be the default. And I suspect it's partly done as a crude solution to the multiline statement problem.
It's easy to parse the source file and find the statement node containing a given line. I've done it in other projects. We'd need to think a bit about what to display for compound statements (e.g.
if
statements) but overall I think it's very doable. Then we can show the whole statement being executed without unwanted additional context. In most cases it would still be just one line.The obvious problem is how to format the display of variables when showing multiple lines of source code. I'd like to brainstorm this issue. Some thoughts:
However horizontal space runs out fast.
a[b]
where evaluating that is safe, e.g. iftype(a) is dict
. The current system of labels would not work well for labelling all three ofa
,b
, anda[b]
.The text was updated successfully, but these errors were encountered: