-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Improve syntax error for invalid comparisons #87963
Comments
Improve syntax error for invalid comparisons such as: >>> if x = 3:
File "<stdin>", line 1
if x = 3:
^
SyntaxError: invalid syntax to print: >>> if x = 3:
File "<stdin>", line 1
if x = 3:
^
SyntaxError: cannot assign to name. Maybe you meant '==' or ':=' instead of '='? instead |
"cannot assign to name" looks wrong. Usually we can assign to name, unless it is a keyword. And the problem is not that we cannot assign to name (we can), but that is is an invalid syntax, assignment is a statement, not an expression. Seems it handles only simplest cases with "=" in "if". It does not handle "while x = 3" and "if y and x = 3". Could it be possible to make it more general. |
I tried to add
in invalid_named_expression, but it does not have any effect. |
These are good points, let me investigate a bit |
That is because the rule for named_expressions are: named_expression[expr_ty]: and the second alternative (| expression !':=') succeeds with an assignment, not letting the invalid_named_expression run |
I have updated the PR with:
>>> f() = 1
File "<stdin>", line 1
f() = 1
^
SyntaxError: cannot assign to function call. Maybe you meant '==' instead of '='? If we still want something different we could say: "cannot assign to ... here". The "here" makes it clear that in other places could be possible to do an assignment (for instance with names or attributes). |
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: