Author Gerrit.Holl
Recipients Gerrit.Holl, lys.nikolaou, pablogsal
Date 2021-05-18.09:49:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1621331398.13.0.330386764302.issue44168@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-05-18 09:49:58Gerrit.Hollsetrecipients: + Gerrit.Holl, lys.nikolaou, pablogsal
2021-05-18 09:49:58Gerrit.Hollsetmessageid: <1621331398.13.0.330386764302.issue44168@roundup.psfhosted.org>
2021-05-18 09:49:58Gerrit.Holllinkissue44168 messages
2021-05-18 09:49:57Gerrit.Hollcreate