Implement multiline dictionary and list hugging for preview style (#8293)

## Summary

This PR implement's Black's new single-argument hugging for lists, sets,
and dictionaries under preview style.

For example, this:

```python
foo(
    [
        1,
        2,
        3,
    ]
)
```

Would instead now be formatted as:

```python
foo([
    1,
    2,
    3,
])
```

A couple notes:

- This doesn't apply when the argument has a magic trailing comma.
- This _does_ apply when the argument is starred or double-starred.
- We don't apply this when there are comments before or after the
argument, though Black does in some cases (and moves the comments
outside the call parentheses).

It doesn't say it in the originating PR
(https://github.com/psf/black/pull/3964), but I think this also applies
to parenthesized expressions? At least, it does in my testing of preview
vs. stable, though it's possible that behavior predated the linked PR.

See: #8279.

## Test Plan

Before:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75804 | 1799 | 1648 |
| django | 0.99984 | 2772 | 34 |
| home-assistant | 0.99963 | 10596 | 146 |
| poetry | 0.99925 | 317 | 12 |
| transformers | 0.99967 | 2657 | 322 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99980 | 3669 | 18 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 21 |

After:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75804 | 1799 | 1648 |
| django | 0.99984 | 2772 | 34 |
| home-assistant | 0.99963 | 10596 | 146 |
| poetry | 0.96215 | 317 | 34 |
| transformers | 0.99967 | 2657 | 322 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99980 | 3669 | 18 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 21 |
This commit is contained in:
Charlie Marsh 2023-11-30 21:11:14 -05:00 committed by GitHub
parent f06c5dc896
commit 019d9aebe9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 1969 additions and 15 deletions

View file

@ -0,0 +1,543 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/simple_cases/preview_hug_parens_with_braces_and_square_brackets.py
---
## Input
```python
def foo_brackets(request):
return JsonResponse(
{
"var_1": foo,
"var_2": bar,
}
)
def foo_square_brackets(request):
return JsonResponse(
[
"var_1",
"var_2",
]
)
func({"a": 37, "b": 42, "c": 927, "aaaaaaaaaaaaaaaaaaaaaaaaa": 11111111111111111111111111111111111111111})
func(["random_string_number_one","random_string_number_two","random_string_number_three","random_string_number_four"])
func(
{
# expand me
'a':37,
'b':42,
'c':927
}
)
func(
[
'a',
'b',
'c',
]
)
func(
[
'a',
'b',
'c',
],
)
func( # a
[ # b
"c", # c
"d", # d
"e", # e
] # f
) # g
func( # a
{ # b
"c": 1, # c
"d": 2, # d
"e": 3, # e
} # f
) # g
func(
# preserve me
[
"c",
"d",
"e",
]
)
func(
[ # preserve me but hug brackets
"c",
"d",
"e",
]
)
func(
[
# preserve me but hug brackets
"c",
"d",
"e",
]
)
func(
[
"c",
# preserve me but hug brackets
"d",
"e",
]
)
func(
[
"c",
"d",
"e",
# preserve me but hug brackets
]
)
func(
[
"c",
"d",
"e",
] # preserve me but hug brackets
)
func(
[
"c",
"d",
"e",
]
# preserve me
)
func([x for x in "short line"])
func([x for x in "long line long line long line long line long line long line long line"])
func([x for x in [x for x in "long line long line long line long line long line long line long line"]])
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"}})
foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
)
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)])
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -47,17 +47,21 @@
],
)
-func([ # a # b
- "c", # c
- "d", # d
- "e", # e
-]) # f # g
+func( # a
+ [ # b
+ "c", # c
+ "d", # d
+ "e", # e
+ ] # f
+) # g
-func({ # a # b
- "c": 1, # c
- "d": 2, # d
- "e": 3, # e
-}) # f # g
+func( # a
+ { # b
+ "c": 1, # c
+ "d": 2, # d
+ "e": 3, # e
+ } # f
+) # g
func(
# preserve me
@@ -95,11 +99,13 @@
# preserve me but hug brackets
])
-func([
- "c",
- "d",
- "e",
-]) # preserve me but hug brackets
+func(
+ [
+ "c",
+ "d",
+ "e",
+ ] # preserve me but hug brackets
+)
func(
[
```
## Ruff Output
```python
def foo_brackets(request):
return JsonResponse({
"var_1": foo,
"var_2": bar,
})
def foo_square_brackets(request):
return JsonResponse([
"var_1",
"var_2",
])
func({
"a": 37,
"b": 42,
"c": 927,
"aaaaaaaaaaaaaaaaaaaaaaaaa": 11111111111111111111111111111111111111111,
})
func([
"random_string_number_one",
"random_string_number_two",
"random_string_number_three",
"random_string_number_four",
])
func({
# expand me
"a": 37,
"b": 42,
"c": 927,
})
func([
"a",
"b",
"c",
])
func(
[
"a",
"b",
"c",
],
)
func( # a
[ # b
"c", # c
"d", # d
"e", # e
] # f
) # g
func( # a
{ # b
"c": 1, # c
"d": 2, # d
"e": 3, # e
} # f
) # g
func(
# preserve me
[
"c",
"d",
"e",
]
)
func([ # preserve me but hug brackets
"c",
"d",
"e",
])
func([
# preserve me but hug brackets
"c",
"d",
"e",
])
func([
"c",
# preserve me but hug brackets
"d",
"e",
])
func([
"c",
"d",
"e",
# preserve me but hug brackets
])
func(
[
"c",
"d",
"e",
] # preserve me but hug brackets
)
func(
[
"c",
"d",
"e",
]
# preserve me
)
func([x for x in "short line"])
func([
x for x in "long line long line long line long line long line long line long line"
])
func([
x
for x in [
x
for x in "long line long line long line long line long line long line long line"
]
])
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",
}
})
foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
)
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)
])
```
## Black Output
```python
def foo_brackets(request):
return JsonResponse({
"var_1": foo,
"var_2": bar,
})
def foo_square_brackets(request):
return JsonResponse([
"var_1",
"var_2",
])
func({
"a": 37,
"b": 42,
"c": 927,
"aaaaaaaaaaaaaaaaaaaaaaaaa": 11111111111111111111111111111111111111111,
})
func([
"random_string_number_one",
"random_string_number_two",
"random_string_number_three",
"random_string_number_four",
])
func({
# expand me
"a": 37,
"b": 42,
"c": 927,
})
func([
"a",
"b",
"c",
])
func(
[
"a",
"b",
"c",
],
)
func([ # a # b
"c", # c
"d", # d
"e", # e
]) # f # g
func({ # a # b
"c": 1, # c
"d": 2, # d
"e": 3, # e
}) # f # g
func(
# preserve me
[
"c",
"d",
"e",
]
)
func([ # preserve me but hug brackets
"c",
"d",
"e",
])
func([
# preserve me but hug brackets
"c",
"d",
"e",
])
func([
"c",
# preserve me but hug brackets
"d",
"e",
])
func([
"c",
"d",
"e",
# preserve me but hug brackets
])
func([
"c",
"d",
"e",
]) # preserve me but hug brackets
func(
[
"c",
"d",
"e",
]
# preserve me
)
func([x for x in "short line"])
func([
x for x in "long line long line long line long line long line long line long line"
])
func([
x
for x in [
x
for x in "long line long line long line long line long line long line long line"
]
])
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",
}
})
foooooooooooooooooooo(
[{c: n + 1 for c in range(256)} for n in range(100)] + [{}], {size}
)
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)
])
```

View file

@ -0,0 +1,752 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/hug.py
---
## Input
```python
# Preview style: hug brackets to call parentheses.
func([1, 2, 3,])
func( # comment
[1, 2, 3,])
func(
# comment
[1, 2, 3,])
func([1, 2, 3,] # comment
)
func([1, 2, 3,]
# comment
)
func([ # comment
1, 2, 3,]
)
func(([1, 2, 3,]))
func(
(
1,
2,
3,
)
)
# Ensure that comprehensions hug too.
func([(x, y,) for (x, y) in z])
# Ensure that dictionaries hug too.
func({1: 2, 3: 4, 5: 6,})
# Ensure that the same behavior is applied to parenthesized expressions.
([1, 2, 3,])
( # comment
[1, 2, 3,])
(
[ # comment
1, 2, 3,])
# Ensure that starred arguments are also hugged.
foo(
*[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
foo(
* # comment
[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
foo(
**[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
foo(
** # comment
[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
# Ensure that multi-argument calls are _not_ hugged.
func([1, 2, 3,], bar)
func([(x, y,) for (x, y) in z], bar)
# Ensure that return type annotations (which use `parenthesize_if_expands`) are also hugged.
def func() -> [1, 2, 3,]:
pass
def func() -> ([1, 2, 3,]):
pass
def func() -> ([1, 2, 3,]):
pass
def func() -> ( # comment
[1, 2, 3,]):
pass
def func() -> (
[1, 2, 3,] # comment
):
pass
def func() -> (
[1, 2, 3,]
# comment
):
pass
# Ensure that nested lists are hugged.
func([
[
1,
2,
3,
]
])
func([
# comment
[
1,
2,
3,
]
])
func([
[
1,
2,
3,
]
# comment
])
func([
[ # comment
1,
2,
3,
]
])
func([ # comment
[
1,
2,
3,
]
])
```
## Output
```python
# Preview style: hug brackets to call parentheses.
func(
[
1,
2,
3,
]
)
func( # comment
[
1,
2,
3,
]
)
func(
# comment
[
1,
2,
3,
]
)
func(
[
1,
2,
3,
] # comment
)
func(
[
1,
2,
3,
]
# comment
)
func(
[ # comment
1,
2,
3,
]
)
func(
(
[
1,
2,
3,
]
)
)
func(
(
1,
2,
3,
)
)
# Ensure that comprehensions hug too.
func(
[
(
x,
y,
)
for (x, y) in z
]
)
# Ensure that dictionaries hug too.
func(
{
1: 2,
3: 4,
5: 6,
}
)
# Ensure that the same behavior is applied to parenthesized expressions.
(
[
1,
2,
3,
]
)
( # comment
[
1,
2,
3,
]
)
(
[ # comment
1,
2,
3,
]
)
# Ensure that starred arguments are also hugged.
foo(
*[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
foo(
# comment
*[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
foo(
**[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
foo(
# comment
**[
a_long_function_name(a_long_variable_name)
for a_long_variable_name in some_generator
]
)
# Ensure that multi-argument calls are _not_ hugged.
func(
[
1,
2,
3,
],
bar,
)
func(
[
(
x,
y,
)
for (x, y) in z
],
bar,
)
# Ensure that return type annotations (which use `parenthesize_if_expands`) are also hugged.
def func() -> (
[
1,
2,
3,
]
):
pass
def func() -> (
[
1,
2,
3,
]
):
pass
def func() -> (
[
1,
2,
3,
]
):
pass
def func() -> ( # comment
[
1,
2,
3,
]
):
pass
def func() -> (
[
1,
2,
3,
] # comment
):
pass
def func() -> (
[
1,
2,
3,
]
# comment
):
pass
# Ensure that nested lists are hugged.
func(
[
[
1,
2,
3,
]
]
)
func(
[
# comment
[
1,
2,
3,
]
]
)
func(
[
[
1,
2,
3,
]
# comment
]
)
func(
[
[ # comment
1,
2,
3,
]
]
)
func(
[ # comment
[
1,
2,
3,
]
]
)
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -1,11 +1,9 @@
# Preview style: hug brackets to call parentheses.
-func(
- [
- 1,
- 2,
- 3,
- ]
-)
+func([
+ 1,
+ 2,
+ 3,
+])
func( # comment
[
@@ -41,61 +39,47 @@
# comment
)
-func(
- [ # comment
- 1,
- 2,
- 3,
- ]
-)
+func([ # comment
+ 1,
+ 2,
+ 3,
+])
-func(
- (
- [
- 1,
- 2,
- 3,
- ]
- )
-)
+func(([
+ 1,
+ 2,
+ 3,
+]))
-func(
- (
- 1,
- 2,
- 3,
- )
-)
+func((
+ 1,
+ 2,
+ 3,
+))
# Ensure that comprehensions hug too.
-func(
- [
- (
- x,
- y,
- )
- for (x, y) in z
- ]
-)
+func([
+ (
+ x,
+ y,
+ )
+ for (x, y) in z
+])
# Ensure that dictionaries hug too.
-func(
- {
- 1: 2,
- 3: 4,
- 5: 6,
- }
-)
+func({
+ 1: 2,
+ 3: 4,
+ 5: 6,
+})
# Ensure that the same behavior is applied to parenthesized expressions.
-(
- [
- 1,
- 2,
- 3,
- ]
-)
+([
+ 1,
+ 2,
+ 3,
+])
( # comment
[
@@ -105,21 +89,17 @@
]
)
-(
- [ # comment
- 1,
- 2,
- 3,
- ]
-)
+([ # comment
+ 1,
+ 2,
+ 3,
+])
# Ensure that starred arguments are also hugged.
-foo(
- *[
- a_long_function_name(a_long_variable_name)
- for a_long_variable_name in some_generator
- ]
-)
+foo(*[
+ a_long_function_name(a_long_variable_name)
+ for a_long_variable_name in some_generator
+])
foo(
# comment
@@ -129,12 +109,10 @@
]
)
-foo(
- **[
- a_long_function_name(a_long_variable_name)
- for a_long_variable_name in some_generator
- ]
-)
+foo(**[
+ a_long_function_name(a_long_variable_name)
+ for a_long_variable_name in some_generator
+])
foo(
# comment
@@ -167,33 +145,27 @@
# Ensure that return type annotations (which use `parenthesize_if_expands`) are also hugged.
-def func() -> (
- [
- 1,
- 2,
- 3,
- ]
-):
+def func() -> ([
+ 1,
+ 2,
+ 3,
+]):
pass
-def func() -> (
- [
- 1,
- 2,
- 3,
- ]
-):
+def func() -> ([
+ 1,
+ 2,
+ 3,
+]):
pass
-def func() -> (
- [
- 1,
- 2,
- 3,
- ]
-):
+def func() -> ([
+ 1,
+ 2,
+ 3,
+]):
pass
@@ -229,56 +201,46 @@
# Ensure that nested lists are hugged.
-func(
+func([
[
- [
- 1,
- 2,
- 3,
- ]
+ 1,
+ 2,
+ 3,
]
-)
+])
-func(
+func([
+ # comment
[
- # comment
- [
- 1,
- 2,
- 3,
- ]
+ 1,
+ 2,
+ 3,
]
-)
+])
-func(
+func([
[
- [
- 1,
- 2,
- 3,
- ]
- # comment
+ 1,
+ 2,
+ 3,
]
-)
+ # comment
+])
-func(
- [
- [ # comment
- 1,
- 2,
- 3,
- ]
+func([
+ [ # comment
+ 1,
+ 2,
+ 3,
]
-)
+])
-func(
- [ # comment
- [
- 1,
- 2,
- 3,
- ]
+func([ # comment
+ [
+ 1,
+ 2,
+ 3,
]
-)
+])
```

View file

@ -194,4 +194,24 @@ response = await sync_to_async(
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -62,9 +62,9 @@
1
}.unicodedata.normalize("NFKCNFKCNFKCNFKCNFKC", s2).casefold()
-ct_match = ([]).unicodedata.normalize("NFKC", s1).casefold() == (
- []
-).unicodedata.normalize("NFKCNFKCNFKCNFKCNFKC", s2).casefold()
+ct_match = ([]).unicodedata.normalize(
+ "NFKC", s1
+).casefold() == ([]).unicodedata.normalize("NFKCNFKCNFKCNFKCNFKC", s2).casefold()
return await self.http_client.fetch(
f"http://127.0.0.1:{self.port}{path}",
```