classification
Title: Improve syntax error for invalid comparisons
Type: Stage: resolved
Components: Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: aroberge, lys.nikolaou, pablogsal, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-04-09 19:11 by pablogsal, last changed 2021-04-13 16:51 by pablogsal. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 25317 merged pablogsal, 2021-04-09 19:13
PR 25375 merged pablogsal, 2021-04-13 01:08
PR 25390 merged pablogsal, 2021-04-13 16:06
Messages (8)
msg390659 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-09 19:11
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
msg390700 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-04-10 07:49
"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.
msg390703 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-04-10 08:31
I tried to add

    | a=NAME '=' {
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
            a, "invalid syntax. Maybe you meant '==' or ':=' instead of '='?") }
    | a=bitwise_or '=' {
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
            a, "invalid syntax. Maybe you meant '==' instead of '='?") }

in invalid_named_expression, but it does not have any effect.
msg390728 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-10 18:04
These are good points, let me investigate a bit
msg390729 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-10 18:08
> in invalid_named_expression, but it does not have any effect.

That is because the rule for named_expressions are:

 named_expression[expr_ty]:
     | a=NAME ':=' ~ b=expression
     | expression !':='
     | invalid_named_expression

and the second alternative (| expression !':=') succeeds with an assignment, not letting the invalid_named_expression run
msg390730 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-10 18:24
I have updated the PR with:

* Works for while statements (we can make it work for other kinds of statements but is a bit tricky because those have different grammar paths. We could do this in other PRs.

* I changed the error message for when the LHS is a name. In other cases we still want the special error message. For example:

>>> 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).
msg390862 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-12 15:59
New changeset b86ed8e3bb41ede77eeab4a8bb4e2b91a8065283 by Pablo Galindo in branch 'master':
bpo-43797: Improve syntax error for invalid comparisons (#25317)
https://github.com/python/cpython/commit/b86ed8e3bb41ede77eeab4a8bb4e2b91a8065283
msg390977 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-13 16:51
New changeset 30ed93bfec5dfa7ee05982e2df8fd810f3f49305 by Pablo Galindo in branch 'master':
bpo-43797: Handle correctly invalid assignments inside function calls and generators (GH-25390)
https://github.com/python/cpython/commit/30ed93bfec5dfa7ee05982e2df8fd810f3f49305
History
Date User Action Args
2021-04-13 16:51:41pablogsalsetmessages: + msg390977
2021-04-13 16:06:36pablogsalsetpull_requests: + pull_request24123
2021-04-13 01:08:46pablogsalsetpull_requests: + pull_request24107
2021-04-12 15:59:58pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-04-12 15:59:34pablogsalsetmessages: + msg390862
2021-04-10 18:41:46pablogsalsetnosy: + lys.nikolaou
2021-04-10 18:24:33pablogsalsetmessages: + msg390730
2021-04-10 18:08:48pablogsalsetmessages: + msg390729
2021-04-10 18:04:33pablogsalsetmessages: + msg390728
2021-04-10 08:31:22serhiy.storchakasetmessages: + msg390703
2021-04-10 07:49:27serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg390700
2021-04-09 19:45:35arobergesetnosy: + aroberge
2021-04-09 19:13:27pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request24052
2021-04-09 19:11:31pablogsalcreate