• Where is the error?

    From Peter J. Holzer@21:1/5 to All on Sun Aug 6 22:41:51 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.
    |_|_) | |
    | | | [email protected] | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmTQBYoACgkQ8g5IURL+ KF0TMA//eWCzwWjvlwtmGmziPkmtCXXhTRfRgjk4KS642h/sCx7m/8GUrSBLLXQ7 LX9RdXCo17M7St1amIMhKCViV09yz7GEMY7j42qCIuvk/ELRgZ71FN9iQ8Jq4ssC 5NHqPkHU2RvoumEHQCIIfcgpSgW31hqx6HZF+9XnGoKtgG2XTyvOhJCCcvePsYir 041atFuyUhfHu5fv+mJxsW/Hvrx+ywlkcmfB46rm5IJ5DZ8LJZ1S1fgGFcZhEmPR AyS8vLpLyHMTG5TX9tRvu0DJchwzWAKDBWEXSgJN30scJh33tnA1lSmVu9FJeHGf AypNyCPieglvYEHGs6yubWL/QrsezF9m8Hjxy43ZI4ofLayX7zTKsIsJrAMgi0Qk VA6YOWXN4qIOZ/1wP4rssV0yx+t89+w9r7V2yUaicudUUERd7xMo1AfdPmE8uk2e hDAq959V/v7JgOyl5eGQbDvairlGNlBjO8hgPO6OqOdCEfIR9kDYpS8tkBfG9Ubg 8oDr2Um9NWnQVGLwc/hPqjPiiLI5YSFIPWDexlTDgYXPJbuYrSb3cFJqFpvAJm1n /JcQnSwyAc5TUh1bSuX+lkk4hEU69b+N1qeLUFsIEhupLIlmU1ErJQ2QCva2BqsV OdLZxOsBPmwkm6AfUoq/QzKyD2ltimHdEur0sTT
  • From dn@21:1/5 to Peter J. Holzer via Python-list on Mon Aug 7 12:18:23 2023
    On 07/08/2023 08.41, Peter J. Holzer via Python-list wrote:
    Mostly, error messages got a lot better in Python 3.10, but this one had
    me scratching my head for a few minutes.
    ...


    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?

    Found myself chuckling at this - not to be unkind, but because can
    easily imagine such confusion on my own part.

    The issue of unhelpful error messages or information aimed at a similar
    but different context, has been the story of our lives. Advice to
    trainees has always been to cast-about looking for the error - rather
    than taking the line-number as a precise location. Have made a note to
    avoid advising folk to work 'backwards'!

    Meantime (back at the ranch?), haven't experienced this. Using an IDE
    means all such stuff is reported, as one types, through highlights and
    squiggly lines (which should(?) be considered and cleared - before
    pressing the GO-button).

    In this case, the PyCharm* editor adds red-squiggles where the commas
    should have been. Hovering the cursor over a squiggle, the IDE reports
    "',' expected"! Its PythonConsole behaves similarly (without offering a 'hover'). Plus, even after the closing brace, it continues to assume a multi-line compound-statement (and thus won't execute, per expected REPL behavior).


    Way-back (grey-beard time!) when we submitted card-decks of code to be
    compiled over-night, one idea to avoid expensive, trivial errors/eras;
    was that spelling-checkers should be built-in to compilers, eg to auto-magically correct typos, eg

    AFF 1 TO TOTAL

    instead of:

    ADD 1 TO TOTAL

    These days, we can look at code from two or more years ago, 'produced'
    by ChatGPT (et al), aka "The Stochastic Parrot". There is some thought
    that artificial 'intelligence' will one-day be able to do the coding for us/predict what is required/act as a crystal ball...

    Speaking of which, and just because I could, here's what Chat-GPT had to
    say when I asked "what's wrong with ...":

    «The issue in the given Python code is that you're missing commas
    between the key-value pairs in the dictionary. Commas are required to
    separate different key-value pairs within a dictionary. Here's the
    corrected version of the code:
    ...
    »

    Question: If a Chat-AI is built into the IDE (I stripped such out from
    mine), does it take-over error reporting and diagnosis (and offer the
    option of replacing with its 'corrected version'?) - rather than
    requiring an extra copy-paste step, per above?
    (and need for my assumption of where the error is located)


    Hope you've exerted copyright over the "clairvoyant" description!


    * JetBrains kindly sponsor our PUG with a monthly door-prize.
    --
    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Peter J. Holzer on Mon Aug 7 14:04:59 2023
    On 06Aug2023 22:41, Peter J. Holzer <[email protected]> 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 <[email protected]>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry@21:1/5 to All on Mon Aug 7 08:02:40 2023
    On 7 Aug 2023, at 05:28, Cameron Simpson via Python-list <[email protected]> wrote:

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

    Was that on DEC VMS? It was a goal at DEC for its compilers to do this well. They could output the errors in a machine readable format to allow editors to auto fix.

    I am learning rust and it is very good at suggesting fixes.
    There is a command to apply fixes automatically, cargo fix.

    Barry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Barry on Mon Aug 7 19:08:00 2023
    On 07Aug2023 08:02, Barry <[email protected]> wrote:
    On 7 Aug 2023, at 05:28, Cameron Simpson via Python-list <[email protected]> wrote:
    Used to use a Pascal compiler once which was uncannily good at
    suggesting where you'd missing a semicolon.

    Was that on DEC VMS? It was a goal at DEC for its compilers to do this well.

    No, a PDP-11 using V7 UNIX.

    They could output the errors in a machine readable format to allow editors to auto fix.
    I am learning rust and it is very good at suggesting fixes.
    There is a command to apply fixes automatically, cargo fix.

    Neat.

    Cheers,
    Cameron Simpson <[email protected]>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Agbenike@21:1/5 to [email protected] on Sun Aug 6 15:59:09 2023
    When i try to open a python script it either says theres no ctk module or
    no pip

    On Sun, Aug 6, 2023, 3:51 PM Peter J. Holzer via Python-list < [email protected]> 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.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.
    |_|_) | |
    | | | [email protected] | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"
    --
    https://mail.python.org/mailman/listinfo/python-list


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)