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 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"])
henriholopainen marked this conversation as resolved.
Show resolved Hide resolved

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

foo(
henriholopainen marked this conversation as resolved.
Show resolved Hide resolved
**{
"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"])
})