classification
Title: lambda expression no longer valid at comp_if in Python 3.9
Type: behavior Stage: resolved
Components: Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gousaiyang, gvanrossum, lys.nikolaou, miss-islington, pablogsal, xtreak
Priority: normal Keywords: patch

Created on 2021-04-06 20:07 by gousaiyang, last changed 2021-04-07 18:18 by pablogsal. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 25231 merged gousaiyang, 2021-04-06 22:03
PR 25233 merged miss-islington, 2021-04-06 22:16
Messages (8)
msg390369 - (view) Author: Saiyang Gou (gousaiyang) * Date: 2021-04-06 20:07
According to the documentation, a lambda expression at the `comp_if` position of a comprehension is allowed (can be parsed as `lambda_expr_nocond`). But this seems broken in Python 3.9 PEG parser. Example:

user@host:/$ python3.8
Python 3.8.9 (default, Apr  3 2021, 01:00:00)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> [x for x in range(10) if lambda: 1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
user@host:/$ python3.9
Python 3.9.3 (default, Apr  3 2021, 00:51:37)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> [x for x in range(10) if lambda: 1]
  File "<stdin>", line 1
    [x for x in range(10) if lambda: 1]
                             ^
SyntaxError: invalid syntax
msg390370 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2021-04-06 20:21
This looks like a duplicate of https://bugs.python.org/issue41848
msg390371 - (view) Author: Saiyang Gou (gousaiyang) * Date: 2021-04-06 20:31
OK I didn't find this duplicate issue. After reading that, I think we should update the documentation (https://docs.python.org/3/reference/expressions.html). `comp_if       ::=  "if" expression_nocond [comp_iter]` can become `comp_if       ::=  "if" or_test [comp_iter]`, and all `nocond` stuffs can be removed.
msg390376 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-04-06 21:21
Oh, nice improvement suggestion for the docs! Do you feel comfortable submitting a small PR for that?
msg390377 - (view) Author: Saiyang Gou (gousaiyang) * Date: 2021-04-06 21:27
I'll work on it soon.
msg390380 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-06 22:15
New changeset 0fdf11e8e901a5f47149232557a7f9726b8177c9 by Saiyang Gou in branch 'master':
bpo-43755: Update docs to reflect that lambda is not allowed in `comp_if` since 3.9 (GH-25231)
https://github.com/python/cpython/commit/0fdf11e8e901a5f47149232557a7f9726b8177c9
msg390381 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-06 22:17
Thanks for the quick fix, Saiyang Gou
msg390459 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-04-07 18:18
New changeset f91fc7a679e076cb9a703f6db4d95b63935562e3 by Miss Islington (bot) in branch '3.9':
bpo-43755: Update docs to reflect that lambda is not allowed in `comp_if` since 3.9 (GH-25231) (GH-25233)
https://github.com/python/cpython/commit/f91fc7a679e076cb9a703f6db4d95b63935562e3
History
Date User Action Args
2021-04-07 18:18:03pablogsalsetmessages: + msg390459
2021-04-06 22:17:20pablogsalsetstatus: open -> closed
resolution: fixed
messages: + msg390381

stage: patch review -> resolved
2021-04-06 22:16:15miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request23970
2021-04-06 22:15:44pablogsalsetmessages: + msg390380
2021-04-06 22:03:13gousaiyangsetkeywords: + patch
stage: patch review
pull_requests: + pull_request23968
2021-04-06 21:27:30gousaiyangsetmessages: + msg390377
2021-04-06 21:21:52gvanrossumsetmessages: + msg390376
2021-04-06 20:33:44xtreaksetnosy: + gvanrossum, lys.nikolaou, pablogsal
2021-04-06 20:31:48gousaiyangsetmessages: + msg390371
2021-04-06 20:21:50xtreaksetnosy: + xtreak
messages: + msg390370
2021-04-06 20:07:25gousaiyangcreate