From 46d97bd05fbbd0a5eaec785b2fcb32c7dd9d6db7 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Fri, 11 Feb 2022 19:14:25 +0300 Subject: [PATCH 1/2] Allow `for`'s target expression to be starred --- CHANGES.md | 3 ++- src/blib2to3/Grammar.txt | 2 +- tests/data/starred_for_target.py | 27 +++++++++++++++++++++++++++ tests/test_format.py | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 tests/data/starred_for_target.py diff --git a/CHANGES.md b/CHANGES.md index e94b345e92a..4e761bed22b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -41,7 +41,8 @@ ### Parser - +- Black can now parse starred expressions in the target of the `for`/`async for` + statements, e.g `for item in *items_1, *items_2: pass` (#2879). ### Performance diff --git a/src/blib2to3/Grammar.txt b/src/blib2to3/Grammar.txt index cf4799f8abe..0ce6cf39111 100644 --- a/src/blib2to3/Grammar.txt +++ b/src/blib2to3/Grammar.txt @@ -109,7 +109,7 @@ compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef async_stmt: ASYNC (funcdef | with_stmt | for_stmt) if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite] while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite] -for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] +for_stmt: 'for' exprlist 'in' testlist_star_expr ':' suite ['else' ':' suite] try_stmt: ('try' ':' suite ((except_clause ':' suite)+ ['else' ':' suite] diff --git a/tests/data/starred_for_target.py b/tests/data/starred_for_target.py new file mode 100644 index 00000000000..8fc8e059ed3 --- /dev/null +++ b/tests/data/starred_for_target.py @@ -0,0 +1,27 @@ +for x in *a, *b: + print(x) + +for x in a, b, *c: + print(x) + +for x in *a, b, c: + print(x) + +for x in *a, b, *c: + print(x) + +async for x in *a, *b: + print(x) + +async for x in *a, b, *c: + print(x) + +async for x in a, b, *c: + print(x) + +async for x in ( + *loooooooooooooooooooooong, + very, + *loooooooooooooooooooooooooooooooooooooooooooooooong, +): + print(x) diff --git a/tests/test_format.py b/tests/test_format.py index 04676c1c2c5..04eda43d5cf 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -62,6 +62,7 @@ ] PY310_CASES: List[str] = [ + "starred_for_target", "pattern_matching_simple", "pattern_matching_complex", "pattern_matching_extras", From 71b57086a68cf16d6a7dcb251ac1d409e03d9bff Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Fri, 11 Feb 2022 20:51:22 +0300 Subject: [PATCH 2/2] Fix typo Co-authored-by: Jelle Zijlstra --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 4e761bed22b..cce1b578210 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -41,7 +41,7 @@ ### Parser -- Black can now parse starred expressions in the target of the `for`/`async for` +- Black can now parse starred expressions in the target of `for` and `async for` statements, e.g `for item in *items_1, *items_2: pass` (#2879). ### Performance