aroberge / friendly

Aimed at Python beginners: replacing standard traceback by something easier to understand
https://friendly-traceback.github.io/docs/index.html
MIT License
325 stars 9 forks source link

SyntaxError with no explanation #228

Closed JulienPalard closed 3 years ago

JulienPalard commented 3 years ago

I really don't know if this is the kind of report you want, I mean : there's an infinity of ways to SyntaxError, trying to handle them all may not be a sane idea... tell me :)

Here what I've seen today (yes, the student "wrote" the spaces up to the 80th column):

Python exception

:::pytb
Traceback (most recent call last):
  File "solution.py", line 1
    print(8958937768937 / 2851718461558) =
                                                                                    ^
SyntaxError: invalid syntax

A SyntaxError occurs when Python cannot understand your code.

Python could not understand the code in the file 'solution.py' beyond the location indicated by ^.

:::text
    -->1: print(8958937768937 / 2851718461558) =
                                                                                          ^

Currently, I cannot guess the likely cause of this error. Try to examine closely the line indicated as well as the line immediately above to see if you can identify some misspelled word, or missing symbols, like (, ), [, ], :, etc.

Unless your code uses type annotations, which are beyond our scope, if you think that this is something which should be handled by friendly, please report this case to https://github.com/aroberge/friendly/issues

aroberge commented 3 years ago

there's an infinity of ways to SyntaxError, trying to handle them all may not be a sane idea... tell me :)

Given the original goal I had in mind and seeing how much further I have gone with no obvious end in sight ..., it is legitimate to question my sanity! ;-)

For SyntaxError: invalid syntax cases, I have adopted the following approach:

  1. Try to identify what looks like common mistakes; (one of) the first one(s) was your suggestion to identify copy-pasted code that contained a prompt.
  2. Keep an eye out for any questions on Reddit, StackOverflow, etc., that I come accross with similar errors.
  3. Try to identify "simple typos" where the addition, substitution or deletion of a single token would yield some valid code.
>>> print(1/2) =

Traceback (most recent call last):
  File "<friendly-console:3>", line 1
    print(1/2) =
                     ^
SyntaxError: invalid syntax
>>> s = _get_statement()
>>> s.bad_token
type=54 (OP)  string='='  start=(1, 11)  end=(1, 12)  line='print(1/2) =     \n'
>>>

If I reproduced the essence of this example correctly, I should have been able to identify and fix it. Checking my code, I have not yet considered deleting tokens, which appears to be the problem in this case (extra =).

aroberge commented 3 years ago

This has been taken care of in the latest version.

JulienPalard commented 3 years ago

I'm always amazed how fast you fix my issues, thanks André!