Skip to content

Commit

Permalink
Hug parens also with multiline unpacking (#3992)
Browse files Browse the repository at this point in the history
  • Loading branch information
henriholopainen committed Oct 30, 2023
1 parent f7cbe4a commit ddfecf0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
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 and dict 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 and dictionary 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
6 changes: 3 additions & 3 deletions src/black/cache.py
Expand Up @@ -124,9 +124,9 @@ def filtered_cached(self, sources: Iterable[Path]) -> Tuple[Set[Path], Set[Path]

def write(self, sources: Iterable[Path]) -> None:
"""Update the cache file data and write a new cache file."""
self.file_data.update(
**{str(src.resolve()): Cache.get_file_data(src) for src in sources}
)
self.file_data.update(**{
str(src.resolve()): Cache.get_file_data(src) for src in sources
})
try:
CACHE_DIR.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile(
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 in [token.STAR, token.DOUBLESTAR] 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,21 @@ 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"])

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

foo(
**{
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1,
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": 2,
"ccccccccccccccccccccccccccccccccc": 3,
**other,
}
)

foo(**{x: y for x, y in enumerate(["long long long long line","long long long long line"])})

# output
def foo_brackets(request):
return JsonResponse({
Expand Down Expand Up @@ -287,3 +302,24 @@ 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)
])

foo(**{
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1,
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": 2,
"ccccccccccccccccccccccccccccccccc": 3,
**other,
})

foo(**{
x: y for x, y in enumerate(["long long long long line", "long long long long line"])
})

0 comments on commit ddfecf0

Please sign in to comment.