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.
Since the code is found in a file, we use python -m friendly_traceback theofficefacts.py to run it and obtain the following.
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.
Friendly traceback provides a "hint" right after the traceback. We can get more information by asking
why().
Misspelling, Missing, or Misusing Python Keywords
Identifying misspelled keywords was actually inspired by that article from Real Python.
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.
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.
In some other cases, like the example given in the Real Python article, Friendly-traceback can identify a missing keyword.
As long as there is only one instance of "in" missing, Friendly-traceback can identify it properly.
Finally, two more cases where a Python keyword is not used properly.
Missing Parentheses, Brackets, and Quotes
Five examples taken from the Real Python article offered without additional comments.
Mistaking Dictionary Syntax
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.
Defining and Calling Functions
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.
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.
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.
No comments:
Post a Comment