I’m using Jupyter lab and sometimes I want to run some assertions on my data. For example, that all the folds have at least 10 test items.
Nevertheless, when the assert fails, I have to modify the notebook and run it again just to print more information.
Is it possible to get verbose asserts like the ones pytest uses:
def test_function():
> assert f() == 4
E assert 3 == 4
E + where 3 = f()
test_assert1.py:6: AssertionError
Perhaps using a jupyter lab plugin or some other Python package.
4
Rich adds a nice traceback that plays great with Jupyter.
Install Rich.
Then in your notebook put as the top cell the following:
from rich.traceback import install
install(show_locals=False)
Run that.
(You can do show_locals=True
if you prefer.)
Then run code in the other cells. The traceback will be better.
You can see examples & read more about an earlier stage of implementation here in Will McGugans’s February 23, 2020 post ‘Better Python tracebacks with Rich’.
He says there about the lines of code shown:
“It’s configurable, but I find that 7 lines of code are enough to make it relatable to the file in my editor, and give me a better understanding of the context that lead to the exception.”
It looks like you can specify the number of surrounding context lines shown to be larger, if you prefer, like so:
from rich.traceback import install
install(show_locals=False, extra_lines=5)
Use of extra_lines
based on here and looking there may give you idea of additional settings you’d like to customize about the tracebacks displayed in your running notebook.
For anyone not using jupyter notebook, the answer is pdb.pm()
— the post mortem debugger.
import pdb; pdb.pm()
However, this does not play nice with Jupyter Notebooks though (at least not the setup I was using). I think it can just about handle input()
, but any other REPL-like behaviour else is too complex for it to handle.
In which case the following function might help:
In [1]: import sys
...:
...: def get_last_exc_locals():
...: try:
...: tb = sys.last_traceback
...: except AttributeError:
...: print('last_traceback not found -- no recorded exception'); return
...: while tb.tb_next:
...: tb = tb.tb_next
...: return tb.tb_frame.f_locals
...:
In [2]: def g(): return 0
...: def f(x):
...: y = g()
...: assert x > y
...:
...: f(-1)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[2], line 6
3 y = g()
4 assert x > y
----> 6 f(-1)
Cell In[2], line 4, in f(x)
2 def f(x):
3 y = g()
----> 4 assert x > y
AssertionError:
In [3]: get_last_exc_locals()
Out[3]: {'x': -1, 'y': 0}
I don’t think you’ll be able to easily do what pytest does, as pytest rewrites source code (the .pyc files) on the fly. It does this so it can implement its special handling for when assert statements fail.