On Thu, Aug 6, 2020 at 3:40 AM Christopher Barker <pythonchb@gmail.com> wrote:
I'm all for clear and helpful error messages, but this feels a tad too verbose (and even a bit patronizing) to me. But the highlighting of character look-alikes is very helpful.

Perhaps something like:

SyntaxError: invalid character in identifier
    -->2:   print(“Squares:”)
An identifier contains one or more unallowed Unicode characters.
It is likely the "curly quote" unicode quotation mark, rather than
the required ASCII single or double quote was used for a string.

I'm assuming that this would detect some of the common "look alikes" and give a specific suggestion as above.

Keep in mind that anyone coding Python is going to see a LOT of SyntaxErrors -- we don't need to present a message as though they've never seen such an error before. But it IS helpful to point out errors that are hard to see with a glance at the code.

You raise some very good points.  Here's a different output from the same program, followed by some explanation for context.
= = =
$ python -im friendly_traceback --verbosity 8 raise_syntax_error75

    SyntaxError: invalid character in identifier

       1: """Should raise SyntaxError: invalid character in identifier for Python <=3.8
       2:    and  SyntaxError: invalid character '«' (U+00AB) in Python 3.9"""
    -->3: a = « hello »

        Python indicates that you used some unicode characters not allowed
        as part of a variable name; this includes many emojis.
        However, I suspect that you used a fancy unicode quotation mark
        instead of a normal single or double quote for a string.
        This can happen if you copy-pasted code.
= = =

1. Friendly-traceback is primarily intended for beginners who do not know what a SyntaxError or a NameError means. This is why it normally includes, by default, [quote] a message as though they've never seen such an error before [/quote].  They also might not know what "an identifier" means, but likely have seen "variable name".

2. Friendly-traceback is designed to support translation. Currently, all the explanations provided are available in French and English, and someone is working on a Chinese translation. Thus there might be a bit of duplication between the message provided by Python ("invalid character in identifier") always shown "as is",  and the longer explanation given by friendly-traceback.

3. It allows one to select a "verbosity level".  Up until a few minutes ago, the available verbosity levels were 0 to 9 - but did not include a level 8 (for historical reasons), each level providing a subset of all possible information. (The default level leaves out the normal Python traceback).  Thanks to your comment, I finally filled a slot for level 8, which is what you see above.  (This addition will be part of the next release on pypi). Note that the location of the ^ might be messed up in this email.

4. The original discussion mentioned that using the wrong types of quotes most often occurred when beginners copy-pasted some code from an ebook or a website. This is why the message mentions it ... but I've modified it so that it is, hopefully, less patronizing.

Thanks again for your comments. Hopefully, the changes I have made are an acceptable compromise between what you suggested and what might be useful for a complete beginner.

André Roberge



Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython