Skip to content
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

Allow for's target expression to be starred #2879

Merged
merged 2 commits into from
Mar 5, 2022
Merged
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
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@

### Parser

<!-- Changes to the parser or to version autodetection -->
- 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

Expand Down
2 changes: 1 addition & 1 deletion src/blib2to3/Grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
27 changes: 27 additions & 0 deletions tests/data/starred_for_target.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
]

PY310_CASES: List[str] = [
"starred_for_target",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs to be 310-specific

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the PEP 617 was opt-out in 3.9, the given examples are rejected by the official AST when the user starts the python interpreter with -Xoldparser. It is why I've added tests to 3.10 group.

"pattern_matching_simple",
"pattern_matching_complex",
"pattern_matching_extras",
Expand Down