Where is the error?

Peter J. Holzer hjp-python at hjp.at
Sun Aug 6 16:41:51 EDT 2023


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.9 (and earlier) reports:

------------------------------------------------------------------------
  File "/home/hjp/tmp/foo", line 3
    "y": (4 + 5 + 6)
    ^
SyntaxError: invalid syntax
------------------------------------------------------------------------

This isn't great, but experience with lots of programming languages
tells me that an error is noticed where or after it actually occurs, so
it's easy to see that there is a comma missing just before the "y".

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. My real program was a bit longer of course, so I
checked the lines before that to see if I forgot to close any
parentheses. Took me some time to notice the missing comma *after* the
underlined expression.

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

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20230806/c5dd6227/attachment.sig>


More information about the Python-list mailing list