Mailing List Archive

Where is the error?
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@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Re: Where is the error? [ In reply to ]
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 <
python-list@python.org> 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.
> |_|_) | |
> | | | hjp@hjp.at | -- Charles Stross, "Creative writing
> __/ | http://www.hjp.at/ | challenge!"
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where is the error? [ In reply to ]
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where is the error? [ In reply to ]
On 06Aug2023 22:41, Peter J. Holzer <hjp-python@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@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Where is the error? [ In reply to ]
> On 7 Aug 2023, at 05:28, Cameron Simpson via Python-list <python-list@python.org> 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




--
https://mail.python.org/mailman/listinfo/python-list