[New-bugs-announce] [issue44168] Wrong syntax error hint when spurious colon present in function keyword comprehension assignment

Gerrit Holl report at bugs.python.org
Tue May 18 05:49:58 EDT 2021


New submission from Gerrit Holl <gerrit.holl at gmail.com>:

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.

----------
components: Parser
messages: 393865
nosy: Gerrit.Holl, lys.nikolaou, pablogsal
priority: normal
severity: normal
status: open
title: Wrong syntax error hint when spurious colon present in function keyword comprehension assignment
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44168>
_______________________________________


More information about the New-bugs-announce mailing list