6

Only Python: Friendly-traceback: testing with Real Python

 3 years ago
source link: https://aroberge.blogspot.com/2021/02/friendly-traceback-testing-with-real.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Saturday, February 27, 2021

Friendly-traceback: testing with Real Python

Real Python is an excellent learning resource for beginning and intermediate Python programmers that want to learn more about various Python related topics. Most of the resources of RealPython are behind a paywall, but there are many articles available for free. One of the free articles, Invalid Syntax in Python: Common Reasons for SyntaxError, is a good overview of possible causes of syntax errors when using Python. The Real Python article shows code raising exceptions due to syntax errors and provides some explanation for each case.

In this blog post, I reproduce the cases covered in the Real Python article and show the information provided by Friendly-traceback. Ideally, you should read this blog post side by side with the Real Python article, as I mostly focus on showing screen captures, with very little added explanation or background.

If you want to follow along using Friendly-traceback, make sure that you use version 0.2.34 or newer.

Missing comma: first example from the article

The article starts off showing some code leading to this rather terse and uninformative traceback.
syntax_error1.png
Since the code is found in a file, we use python -m friendly_traceback theofficefacts.py to run it and obtain the following.
syntax_error1a.png

Misusing the Assignment Operator (=)

We only show one example here, as the others mentioned in the article would be redundant. We remind you for one last time that, if you are not doing so, you should really look at the Real Python article at the same time as you go through this rather terse blog post.
syntax_error2.png
Friendly traceback provides a "hint" right after the traceback. We can get more information by asking why().
syntax_error2a.png

Misspelling, Missing, or Misusing Python Keywords

Identifying misspelled keywords was actually inspired by that article from Real Python.
syntax_error3.png
syntax_error3a.png
Note that Friendly-traceback identifies "for" as being the most likely misspelled keyword, but gives other possible valid choices.
Friendly-traceback can also identify when break (and return) are used outside a loop.
syntax_error4.png
syntax_error4a.png
To the English reader, Friendly-traceback might seem to add very little useful information. However, keep in mind that all this additional information can be translated. If you read the following and do not understand what "boucle" means, then you might get an idea of the some of the challenges faced by non-English speakers when using Python.
syntax_error4b.png
In some other cases, like the example given in the Real Python article, Friendly-traceback can identify a missing keyword.
syntax_error4c.png
As long as there is only one instance of "in" missing, Friendly-traceback can identify it properly.
syntax_error4d.png
Finally, two more cases where a Python keyword is not used properly.
syntax_error5.png
syntax_error6.png

Missing Parentheses, Brackets, and Quotes

Five examples taken from the Real Python article offered without additional comments.
syntax_error7.png
syntax_error8.png
syntax_error9.png
syntax_error10.png
syntax_error11.png

Mistaking Dictionary Syntax

syntax_error12.png

Using the Wrong Indentation

Real Python gives many examples. They would all be handled correctly by Friendly-traceback in a similar way as the single example we decided to use for this post.
syntax_error13.png

Defining and Calling Functions

syntax_error14.png
syntax_error15.png

Changing Python Versions

syntax_error16.png
Friendly-traceback requires Python version 3.6 or newer. Not shown here is that it can recognize that the walrus operator, :=, is not valid before Python version 3.8 and give an appropriate message.

Last example: TypeError result of a syntax error.

Let's look at the last example in the Real Python article.
syntax_error17.png
The explanation given by Friendly-traceback might seem weird "the object (1, 2) was meant to be a function ...".  Often one might have assigned a name to that object, which leads to an explanation that should be seen as more reasonable.
syntax_error17a.png
syntax_error17b.png
The explanation of looking for a "missing comma" when this TypeError is raised was actually added following a suggestion by S. de Menten in the recent contest I held for Friendly-traceback.

There is more ...

Friendly-traceback includes many more cases that those shown above and mentioned in the Real Python article. However, it is limited in that it can only identify the cause of syntax errors there is a single word or symbol used incorrectly or if the error message provided by Python is more informative than the dreaded SyntaxError: invalid syntax.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK