Skip to content

bpo-41848: Allow simple lambdas in if clauses of comprehensions #22394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ expression[expr_ty] (memo):
| disjunction
| lambdef

expression_no_cond[expr_ty]:
| disjunction
| 'lambda' a=[lambda_params] ':' b=expression_no_cond { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) }

lambdef[expr_ty]:
| 'lambda' a=[lambda_params] ':' b=expression { _Py_Lambda((a) ? a : CHECK(_PyPegen_empty_arguments(p)), b, EXTRA) }

Expand Down Expand Up @@ -521,9 +525,9 @@ kvpair[KeyValuePair*]: a=expression ':' b=expression { _PyPegen_key_value_pair(p
for_if_clauses[asdl_comprehension_seq*]:
| a[asdl_comprehension_seq*]=for_if_clause+ { a }
for_if_clause[comprehension_ty]:
| ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
| ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=expression_no_cond { z })* {
CHECK_VERSION(6, "Async comprehensions are", _Py_comprehension(a, b, c, 1, p->arena)) }
| 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
| 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=expression_no_cond { z })* {
_Py_comprehension(a, b, c, 0, p->arena) }
| invalid_for_target

Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -2005,6 +2005,19 @@ async def f():
run_async(f()),
([], {1: 1, 2: 2, 3: 3}))

def test_comp_9(self):
async def gen():
yield 1
yield 2

async def f():
l = [i async for i in gen() if lambda: None]
return [i for i in l]

self.assertEqual(
run_async(f()),
([], [1, 2]))

def test_copy(self):
async def func(): pass
coro = func()
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@
>>> test_func()
[2, 2, 2, 2, 2]

########### Tests for different syntactic corner cases ############

>>> [x for x in [] if lambda: x]
[]

>>> [x for x in [] if lambda: x or b]
[]

>>> [x for x in [True, True, False] if x or True]
[True, True, False]

>>> [y for x in [1, 1, 0] if (y := x + 1)]
[2, 2, 1]

"""


Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,32 @@
Traceback (most recent call last):
SyntaxError: only single target (not list) can be annotated

>>> [x for x in range(5) if a:=b]
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> [x for x in range(5) if if a else b]
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> [x for x in range(5) if lambda: a:=b]
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> [x for x in range(5) if lambda: True if True else False]
Traceback (most recent call last):
SyntaxError: invalid syntax

>>> [x async for x in range(5) if a:=b]
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> [x async for x in range(5) if if a else b]
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> [x async for x in range(5) if lambda: a:=b]
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> [x async for x in range(5) if lambda: True if True else False]
Traceback (most recent call last):
SyntaxError: invalid syntax

Corner-cases that used to fail to raise the correct error:

>>> def f(*, x=lambda __debug__:0): pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow simple lambda expressions in the if clauses of the comprehensions.
Loading