Where is the error?

Cameron Simpson cs at cskk.id.au
Mon Aug 7 00:04:59 EDT 2023


On 06Aug2023 22:41, Peter J. Holzer <hjp-python at hjp.at> wrote:
>Mostly, error messages got a lot better in Python 3.10, but this one had
>me scratching my head for a few minutes.
>
>Consider this useless and faulty script:
>------------------------------------------------------------------------
>r = {
>    "x": (1 + 2 + 3)
>    "y": (4 + 5 + 6)
>    "z": (7 + 8 + 9)
>}
>------------------------------------------------------------------------
[...]
>Python 3.10 and 3.11 report:
>
>------------------------------------------------------------------------
>  File "/home/hjp/tmp/foo", line 2
>    "x": (1 + 2 + 3)
>          ^^^^^^^^^^
>SyntaxError: invalid syntax. Perhaps you forgot a comma?
>------------------------------------------------------------------------
>
>The error message is now a lot better, of course, but the fact that it
>points at the expression *before* the error completely threw me. The
>underlined expression is clearly not missing a comma, nor is there an
>error before that.

Well, it's hard to underline a token which isn't present. But maybe the 
message could be more evocative:

     SyntaxError: invalid syntax. Perhaps you forgot a comma after the underlined code?

>Is this "clairvoyant" behaviour a side-effect of the new parser or was
>that a deliberate decision?

I have the vague impression the new parser enabled the improved 
reporting.

Used to use a Pascal compiler once which was uncannily good at 
suggesting where you'd missing a semicolon.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list