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

Hug parens also with multiline unpacking #3992

Merged
merged 12 commits into from Oct 30, 2023
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -14,6 +14,8 @@

- Multiline dictionaries and lists that are the sole argument to a function are now
indented less (#3964)
- Multiline list unpacking as the sole argument to a function is now also indented less
(#3992)

### Configuration

Expand Down
20 changes: 20 additions & 0 deletions docs/the_black_code_style/future_style.md
Expand Up @@ -139,6 +139,26 @@ foo([
])
```

This also applies to list unpacking:

```python
foo(
*[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
```

will become:

```python
foo(*[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
])
```

You can use a magic trailing comma to avoid this compacting behavior; by default,
_Black_ will not reformat the following code:

Expand Down
7 changes: 4 additions & 3 deletions src/black/linegen.py
Expand Up @@ -817,16 +817,17 @@ def _first_right_hand_split(
head_leaves.reverse()

if Preview.hug_parens_with_braces_and_square_brackets in line.mode:
is_unpacking = 1 if body_leaves[0].type == token.STAR else 0
if (
tail_leaves[0].type == token.RPAR
and tail_leaves[0].value
and tail_leaves[0].opening_bracket is head_leaves[-1]
and body_leaves[-1].type in [token.RBRACE, token.RSQB]
and body_leaves[-1].opening_bracket is body_leaves[0]
and body_leaves[-1].opening_bracket is body_leaves[is_unpacking]
):
head_leaves = head_leaves + body_leaves[:1]
head_leaves = head_leaves + body_leaves[: 1 + is_unpacking]
tail_leaves = body_leaves[-1:] + tail_leaves
body_leaves = body_leaves[1:-1]
body_leaves = body_leaves[1 + is_unpacking : -1]

head = bracket_split_build_line(
head_leaves, line, opening_bracket, component=_BracketSplitComponent.head
Expand Down
Expand Up @@ -137,6 +137,10 @@ def foo_square_brackets(request):
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {x}, "a string", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)

foo(*["long long long long long line", "long long long long long line", "long long long long long line"])
henriholopainen marked this conversation as resolved.
Show resolved Hide resolved

foo(*[str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)])

# output
def foo_brackets(request):
return JsonResponse({
Expand Down Expand Up @@ -287,3 +291,13 @@ def foo_square_brackets(request):
baaaaaaaaaaaaar(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {x}, "a string", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)

foo(*[
"long long long long long line",
"long long long long long line",
"long long long long long line",
])

foo(*[
str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)
])