Friday, December 18, 2020

Friendly-traceback of the day (Dec. 18/20)



Feel free to make suggestions as to what other Exception could benefit from a Friendly-traceback treatment.

Thursday, December 17, 2020

pytest apparently modifies calls to range

 As I work making Friendly-traceback provide more useful information regarding the cause of an exception, I sometimes encounter weird "corner cases" about either Python itself [1] or occasionally about pytest [2]. Today, it was pytest's turn to give me a new puzzle to solve.

Consider the following:

range(1.0)

If you run this, Python will give you a TypeError: 'float' object cannot be interpreted as an integer

Using Friendly-traceback's console [3], I get something slightly more informative.


Actually, I can get even more information using "explain()":


Time to add this new working case to the unit test suite. I create a barebone one for the purpose of this blog, without capturing the output and comparing with what is expected.

import friendly_traceback

def test():
    try:
        range(1.0)
    except:
        friendly_traceback.explain_traceback()

if __name__ == '__main__':
    test()

Here's what happens if I run this using Python:


(1) is a "hint" that gets added to the traceback shown by Friendly-traceback. (2) is part of the more detailed explanation, available by typing "why()" in a Friendly console. So far, everything looks as expected.

In order to provide this kind of explanation, Friendly-traceback looks at the content of the frame where the exception was raised and does its best to deduce what needs to be changed. For example, if we modify the above example to use a variable called "end" instead of using a literal, here's what we get.

When writing unit tests, I make sure that such highlighted hints are reproduced exactly in the output that is captured. So, what happens if we run the previous example using pytest? ...


A new float variable, "start", has suddenly appeared, seemingly out of nowhere. Note that this pytest oddity is not revealed if we use a string instead of a float as the wrong type of argument.


Time to move on to handling other cases ...

[1] See this blogpost.

[2] An issue that I filed about a previous case seems to have disappeared and all that remains is this question on Stack Overflow.

[3] This is my local development version; the example shown here will be handled by versions 0.2.8 and later, to be released on pypi.

Wednesday, December 09, 2020

IPython and Friendly-traceback

 This is a quick update on the status of Friendly-traceback. As of today, it works with the IPython console, and works even better in a JupyterLab environment.  

First, a comparison with using it in the new Windows terminal making use of Rich for syntax highlighting, which is something that has been working for quite a while now.



Next, is the same example in an IPython console, running in a Windows terminal.



I've tried to use Rich to do syntax colouring with the IPython console ... but the result is really disappointing. 








Finally, here's what it looks like in the JupyterLab environment.



Right now, it is not possible to select a different theme for syntax highlighting; this will have to wait until later.


To find out more about Friendly-traceback (excluding this new IPython experimental support), please consult the documentation.




Saturday, October 24, 2020

Friendly-traceback: work in progress

 It's been almost two months since my last blog post and I feel guilty of not having taken the time to write more regularly.  I should really tell you about how fantastic Will McGugan's Rich is, and how I have customized it for my projects. I should also tell you how Sylvain Desodt's DidYouMeanPython has been influencing Friendly-traceback latest developments. Also worthy of note is how Alex Hall's FutureCoder project is incorporating so many neat tools that it feels like a real honour that he has incorporated Friendly-traceback in it.

Alas, while I have been busy making many changes and addition to the code, the documentation is hopelessly behind and no longer gives a correct picture of what Friendly-traceback is now capable of.

So much to do, so little time. So, I will just end with a picture, and go back to coding, with a promise of writing more ... soon I hope.



Monday, August 10, 2020

Rich + Friendly-traceback: first look

 After a couple of hours of work, I have been able to use Rich to add colour to Friendly-traceback. Rich is a fantastic project, which has already gotten a fair bit of attention and deserves even more.

The following is just a preview of things to come; it is just a quick proof of concept.


Friendly-traceback has 10 so-called verbosity settings, one of which is simply to show the normal Python traceback.

There is, of course, much, much more to come ...

Update: more work in progress