classification
Title: Wrong syntax error hint when spurious colon present in function keyword comprehension assignment
Type: Stage: resolved
Components: Parser Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Gerrit.Holl, lys.nikolaou, miss-islington, pablogsal
Priority: normal Keywords: patch

Created on 2021-05-18 09:49 by Gerrit.Holl, last changed 2021-05-19 18:28 by pablogsal. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 26210 merged pablogsal, 2021-05-18 10:27
PR 26247 merged miss-islington, 2021-05-19 18:03
PR 26250 merged pablogsal, 2021-05-19 18:09
Messages (3)
msg393865 - (view) Author: Gerrit Holl (Gerrit.Holl) * Date: 2021-05-18 09:49
In Python 3.9.4, in a function call, when assigning a comprehension to a keyword argument, and the comprehension contains a spurious colon (if a list or set comprehension) or a missing value (when a dict comprehension), the syntax error message gives a hint that incorrectly suggests that the expression cannot contain an assignment:

$ python
Python 3.9.4 | packaged by conda-forge | (default, May 10 2021, 22:13:33)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f(a={x: for x in {}})
  File "<stdin>", line 1
    f(a={x: for x in {}})
      ^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

I certainly didn't mean "==" here.  The syntax is correct if I either remove the :, or add a value after the :, such as:

>>> f(a={x:x for x in {}})
>>> f(a={x for x in {}})

(correct syntax, succeeds if f is defined).

The problem here has nothing to do with assignments being in the wrong place, so the message reported by cpython is incorrect.

In Python 3.8.10 Python simply and correctly states that the syntax is incorrect:

>>> f(a={x: for x in {}})
  File "<stdin>", line 1
    f(a={x: for x in {}})
            ^
SyntaxError: invalid syntax

I have not tried Python 3.10b1.
msg393962 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-19 18:27
New changeset d4a9264ab899b4f3088b5afad8956123924a5ab1 by Pablo Galindo in branch '3.9':
[3.9] bpo-44168: Fix error message in the parser for keyword arguments for invalid expressions (GH-26210) (GH-26250)
https://github.com/python/cpython/commit/d4a9264ab899b4f3088b5afad8956123924a5ab1
msg393963 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-05-19 18:28
New changeset ec0699c044b4272a1face752fd39b8230f96c0da by Miss Islington (bot) in branch '3.10':
bpo-44168: Fix error message in the parser for keyword arguments for invalid expressions (GH-26210) (GH-26247)
https://github.com/python/cpython/commit/ec0699c044b4272a1face752fd39b8230f96c0da
History
Date User Action Args
2021-05-19 18:28:35pablogsalsetmessages: + msg393963
2021-05-19 18:27:06pablogsalsetmessages: + msg393962
2021-05-19 18:13:27pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-05-19 18:09:24pablogsalsetpull_requests: + pull_request24861
2021-05-19 18:03:18miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24860
2021-05-18 10:27:17pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request24827
2021-05-18 09:49:58Gerrit.Hollcreate