Skip to content

Commit

Permalink
Fix psf#4011: Handle more huggable immediately nested parens/brackets.
Browse files Browse the repository at this point in the history
  • Loading branch information
yilei committed Nov 1, 2023
1 parent e501103 commit 87ae9b4
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 27 deletions.
4 changes: 2 additions & 2 deletions CHANGES.md
Expand Up @@ -13,8 +13,8 @@

### Preview style

- Multiline dictionaries and lists that are the sole argument to a function are now
indented less (#3964)
- Multiline tuples, lists, and dictionaries that are the sole argument to a function
or inside another tuple, list, or dictionary are now indented less (#3964, #4012)
- Multiline list and dict unpacking as the sole argument to a function is now also
indented less (#3992)

Expand Down
17 changes: 15 additions & 2 deletions docs/the_black_code_style/future_style.md
Expand Up @@ -116,8 +116,7 @@ my_dict = {
### Improved multiline dictionary and list indentation for sole function parameter

For better readability and less verticality, _Black_ now pairs parentheses ("(", ")")
with braces ("{", "}") and square brackets ("[", "]") on the same line for single
parameter function calls. For example:
with braces ("{", "}") and square brackets ("[", "]") on the same line. For example:

```python
foo(
Expand All @@ -127,6 +126,14 @@ foo(
3,
]
)

nested_array = [
[
1,
2,
3,
]
]
```

will be changed to:
Expand All @@ -137,6 +144,12 @@ foo([
2,
3,
])

nested_array = [[
1,
2,
3,
]]
```

This also applies to list and dictionary unpacking:
Expand Down
15 changes: 9 additions & 6 deletions src/black/linegen.py
Expand Up @@ -817,18 +817,21 @@ def _first_right_hand_split(
body_leaves.reverse()
head_leaves.reverse()

if Preview.hug_parens_with_braces_and_square_brackets in line.mode:
if (
Preview.hug_parens_with_braces_and_square_brackets in line.mode
and tail_leaves[0].value
and tail_leaves[0].opening_bracket is head_leaves[-1]
):
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]
while (
len(body_leaves) > 2 + is_unpacking
and body_leaves[-1].type in CLOSING_BRACKETS
and body_leaves[-1].opening_bracket is body_leaves[is_unpacking]
):
head_leaves = head_leaves + body_leaves[: 1 + is_unpacking]
tail_leaves = body_leaves[-1:] + tail_leaves
body_leaves = body_leaves[1 + is_unpacking : -1]
is_unpacking = 0

head = bracket_split_build_line(
head_leaves, line, opening_bracket, component=_BracketSplitComponent.head
Expand Down
Expand Up @@ -128,6 +128,9 @@ def foo_square_brackets(request):
func({"short line"})
func({"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"})
func({{"long line", "long long line", "long long long line", "long long long long line", "long long long long long line"}})
func(("long line", "long long line", "long long long line", "long long long long line", "long long long long long line"))
func((("long line", "long long line", "long long long line", "long long long long line", "long long long long long line")))
func([["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]])

foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
Expand All @@ -137,6 +140,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]
)

nested_mapping = {"key": [{"a very long key 1": "with a very long value", "a very long key 2": "with a very long value"}]}
nested_array = [[["long line", "long long line", "long long long line", "long long long long line", "long long long long long line"]]]
explicit_exploding = [[["short", "line",],],]

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)])
Expand Down Expand Up @@ -285,15 +292,34 @@ def foo_square_brackets(request):
"long long long long line",
"long long long long long line",
})
func({
{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}
})
func({{
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
}})
func((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
))
func(((
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
)))
func([[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]])

foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
Expand All @@ -303,6 +329,28 @@ 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]
)

nested_mapping = {
"key": [{
"a very long key 1": "with a very long value",
"a very long key 2": "with a very long value",
}]
}
nested_array = [[[
"long line",
"long long line",
"long long long line",
"long long long long line",
"long long long long long line",
]]]
explicit_exploding = [
[
[
"short",
"line",
],
],
]

foo(*[
"long long long long long line",
"long long long long long line",
Expand Down
15 changes: 7 additions & 8 deletions tests/data/cases/preview_long_strings__regression.py
Expand Up @@ -611,14 +611,13 @@ def foo():

class A:
def foo():
XXXXXXXXXXXX.append(
(
"xxx_xxxxxxxxxx(xxxxx={}, xxxx={}, xxxxx, xxxx_xxxx_xxxxxxxxxx={})"
.format(xxxxx, xxxx, xxxx_xxxx_xxxxxxxxxx),
my_var,
my_other_var,
)
)
XXXXXXXXXXXX.append((
"xxx_xxxxxxxxxx(xxxxx={}, xxxx={}, xxxxx, xxxx_xxxx_xxxxxxxxxx={})".format(
xxxxx, xxxx, xxxx_xxxx_xxxxxxxxxx
),
my_var,
my_other_var,
))


class A:
Expand Down

0 comments on commit 87ae9b4

Please sign in to comment.