This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: SyntaxError misidentified in 3.10.0b1 when = used instead of : in dict literal
Type: Stage: patch review
Components: Parser Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, lys.nikolaou, miss-islington, pablogsal, shreyanavigyan
Priority: normal Keywords: patch

Created on 2021-05-19 18:38 by aroberge, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 26253 merged pablogsal, 2021-05-19 19:35
PR 26281 merged miss-islington, 2021-05-21 15:10
PR 26283 merged pablogsal, 2021-05-21 15:43
PR 26292 merged miss-islington, 2021-05-21 17:35
Messages (10)
msg393964 - (view) Author: Andre Roberge (aroberge) * Date: 2021-05-19 18:38
When an equal sign is used instead of a colon in creating a dict literal, depending on the context, either the "bad token" is misidentified OR the would-be helpful error message is incorrect in this particular case.

1) Example of bad token.
Previously, the = sign was identified correctly:

>>> ages = {'Alice'=22, 'Bob'=23}
  File "<stdin>", line 1
    ages = {'Alice'=22, 'Bob'=23}
                   ^
SyntaxError: invalid syntax

With Python 3.10.0b1, the comma is identified as the bad token:

>>> ages = {'Alice'=22, 'Bob'=23}
  File "<stdin>", line 1
    ages = {'Alice'=22, 'Bob'=23}
                      ^
SyntaxError: invalid syntax


2) Example of incorrect error message

Previously, we got the traditional and unhelpful "invalid syntax" but with the bad token identified correctly:

>>> ages = {'Alice'=22}
  File "<stdin>", line 1
    ages = {'Alice'=22}
                   ^
SyntaxError: invalid syntax

With Python 3.10.0b1, we get the following:

>>> ages = {'Alice'=22}
  File "<stdin>", line 1
    ages = {'Alice'=22}
            ^^^^^^^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

I suspect that the ratio (== suggestion correct/ : suggestion correct) would be vanishingly small. ;-)
msg393965 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-19 19:14
The bad token example needs a fix but the second is actually ok because we have another type we write within curly braces "{" and there the error is actually perfect. Like if we follow the error message, we use

ages = {'Alice'==22}

And we actually get a "set" with the first value as False. Sets behaves syntactically like list but have different behaviors of mathematical sets.
msg393966 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-19 19:31
I have an idea of what's causing the first one, but the second I am not sure we can detect easily we are in that case. Do you have a suggestion for what to place in the second case?
msg393967 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-19 19:34
I'm don't the second is a problem at all. What the error message is suggesting is perfect since it doesn't know if we're trying to use a set or dict.
msg393968 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-19 19:35
Sorry for the typos.
msg393969 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-19 19:37
I don't know, it think is more likely that people are triying to use the "=" as a ":" and constructing a literal. Is not a bad error, but I think is not the best one
msg393970 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-19 19:41
Hmmm... Then is it possible to add an exception where the error message will result in,

SyntaxError: cannot assign to literal here. Maybe you meant ':', "==" or ":=" instead of '='?

only if we're dealing with curly braces?
msg393971 - (view) Author: Andre Roberge (aroberge) * Date: 2021-05-19 19:46
In the second case, I understand very well that it could have been a set literal. In my (limited) experience, I have never seen a set literal containing a single element obtained from an == comparison.

Since dict can be built by using keyword arguments, I tend to assume that using = in an literal that starts with { is meant to be a dict.

In
>>> ages = {'Alice' = 22}

replacing the equal sign by either ==, :, or a comma would generate no SyntaxError.  Clearly (in my mind anyway, and in previous Python versions), the "bad token" is the equal sign, and not the string Alice.

Here's what I show with friendly:

======
>>> ages = {'Alice'=22}

Traceback (most recent call last):
  File "<friendly-console:1>", line 1
    ages = {'Alice'=22}
                   ^
SyntaxError: invalid syntax
>>> why()

It is possible that you used an equal sign = instead of a colon : to assign values to keys in a dict before or at the
position indicated by ^.

=====
Admitedly, this suggestion could also be wrong - but the focus on this case (imo) should be on the "bad token" shown, which should be the equal sign.
msg394119 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-21 15:30
New changeset 07dba474c582e61ca455846da7597d4346324e04 by Miss Islington (bot) in branch '3.10':
bpo-44180: Report generic syntax errors in the furthest position reached in the first parser pass (GH-26253) (GH-26281)
https://github.com/python/cpython/commit/07dba474c582e61ca455846da7597d4346324e04
msg394139 - (view) Author: miss-islington (miss-islington) Date: 2021-05-21 18:20
New changeset ae1732d4611ee859c754e7a867b2a4cbb47d0637 by Miss Islington (bot) in branch '3.10':
bpo-44180: Fix edge cases in invalid assigment rules in the parser (GH-26283)
https://github.com/python/cpython/commit/ae1732d4611ee859c754e7a867b2a4cbb47d0637
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88346
2021-05-21 18:20:51miss-islingtonsetmessages: + msg394139
2021-05-21 17:35:03miss-islingtonsetpull_requests: + pull_request24896
2021-05-21 15:43:27pablogsalsetpull_requests: + pull_request24889
2021-05-21 15:30:10pablogsalsetmessages: + msg394119
2021-05-21 15:10:08miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24887
2021-05-19 19:46:23arobergesetmessages: + msg393971
2021-05-19 19:41:46shreyanavigyansetmessages: + msg393970
2021-05-19 19:37:55pablogsalsetmessages: + msg393969
2021-05-19 19:35:29shreyanavigyansetmessages: + msg393968
2021-05-19 19:35:02pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request24862
2021-05-19 19:34:40shreyanavigyansetmessages: + msg393967
2021-05-19 19:31:12pablogsalsetmessages: + msg393966
2021-05-19 19:14:32shreyanavigyansetnosy: + shreyanavigyan
messages: + msg393965
2021-05-19 18:38:05arobergecreate