-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SyntaxError misidentified in 3.10.0b1 when = used instead of : in dict literal #88346
Comments
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.
>>> 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
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. ;-) |
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. |
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? |
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. |
Sorry for the typos. |
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 |
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? |
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: ======
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 ===== |
I think this is resolved, the error now is:
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: