prefer_splitting_right_hand_side_of_assignments preview style (#8943)

This commit is contained in:
Micha Reiser 2023-12-13 12:43:23 +09:00 committed by GitHub
parent 1a65e544c5
commit 45f603000d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 1473 additions and 332 deletions

View file

@ -95,7 +95,7 @@ def f(
```diff
--- Black
+++ Ruff
@@ -7,26 +7,16 @@
@@ -7,23 +7,13 @@
)
# "AnnAssign"s now also work
@ -120,16 +120,10 @@ def f(
- | Loooooooooooooooooooooooong
- | Loooooooooooooooooooooooong
-) = 7
-z: Short | Short2 | Short3 | Short4 = 8
-z: int = 2.3
-z: int = foo()
+z: Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong = 7
+z: (Short | Short2 | Short3 | Short4) = 8
+z: (int) = 2.3
+z: (int) = foo()
# In case I go for not enforcing parantheses, this might get improved at the same time
x = (
z: Short | Short2 | Short3 | Short4 = 8
z: int = 2.3
z: int = foo()
@@ -63,7 +53,7 @@
@ -186,9 +180,9 @@ z: (int)
z: Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong | Loooooooooooooooooooooooong = 7
z: (Short | Short2 | Short3 | Short4) = 8
z: (int) = 2.3
z: (int) = foo()
z: Short | Short2 | Short3 | Short4 = 8
z: int = 2.3
z: int = foo()
# In case I go for not enforcing parantheses, this might get improved at the same time
x = (

View file

@ -789,14 +789,12 @@ log.info(f"""Skipping: {'a' == 'b'} {desc['ms_name']} {money=} {dte=} {pos_share
- "This is a large string that has a type annotation attached to it. A type"
- " annotation should NOT stop a long string from being wrapped."
-)
-annotated_variable: Literal["fakse_literal"] = (
+annotated_variable: Final = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
annotated_variable: Literal["fakse_literal"] = (
- "This is a large string that has a type annotation attached to it. A type"
- " annotation should NOT stop a long string from being wrapped."
-)
+annotated_variable: Final = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
+annotated_variable: Literal[
+ "fakse_literal"
+] = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
+ "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
)
-backslashes = (
- "This is a really long string with \"embedded\" double quotes and 'single' quotes"
@ -1308,9 +1306,9 @@ annotated_variable: Final = (
+ "using the '+' operator."
)
annotated_variable: Final = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
annotated_variable: Literal[
"fakse_literal"
] = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
annotated_variable: Literal["fakse_literal"] = (
"This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
)
backslashes = "This is a really long string with \"embedded\" double quotes and 'single' quotes that also handles checking for an even number of backslashes \\"
backslashes = "This is a really long string with \"embedded\" double quotes and 'single' quotes that also handles checking for an even number of backslashes \\\\"

View file

@ -832,7 +832,7 @@ s = f'Lorem Ipsum is simply dummy text of the printing and typesetting industry:
some_commented_string = ( # This comment stays at the top.
"This string is long but not so long that it needs hahahah toooooo be so greatttt"
@@ -279,38 +280,27 @@
@@ -279,36 +280,25 @@
)
lpar_and_rpar_have_comments = func_call( # LPAR Comment
@ -852,33 +852,31 @@ s = f'Lorem Ipsum is simply dummy text of the printing and typesetting industry:
- f" {'' if ID is None else ID} | perl -nE 'print if /^{field}:/'"
-)
+cmd_fstring = f"sudo -E deluge-console info --detailed --sort-reverse=time_added {'' if ID is None else ID} | perl -nE 'print if /^{field}:/'"
+
+cmd_fstring = f"sudo -E deluge-console info --detailed --sort-reverse=time_added {'{{}}' if ID is None else ID} | perl -nE 'print if /^{field}:/'"
-cmd_fstring = (
- "sudo -E deluge-console info --detailed --sort-reverse=time_added"
- f" {'{{}}' if ID is None else ID} | perl -nE 'print if /^{field}:/'"
-)
+cmd_fstring = f"sudo -E deluge-console info --detailed --sort-reverse=time_added {{'' if ID is None else ID}} | perl -nE 'print if /^{field}:/'"
+cmd_fstring = f"sudo -E deluge-console info --detailed --sort-reverse=time_added {'{{}}' if ID is None else ID} | perl -nE 'print if /^{field}:/'"
-cmd_fstring = (
- "sudo -E deluge-console info --detailed --sort-reverse=time_added {'' if ID is"
- f" None else ID}} | perl -nE 'print if /^{field}:/'"
-)
+fstring = f"This string really doesn't need to be an {{{{fstring}}}}, but this one most certainly, absolutely {does}."
+cmd_fstring = f"sudo -E deluge-console info --detailed --sort-reverse=time_added {{'' if ID is None else ID}} | perl -nE 'print if /^{field}:/'"
+fstring = f"This string really doesn't need to be an {{{{fstring}}}}, but this one most certainly, absolutely {does}."
+
fstring = (
- "This string really doesn't need to be an {{fstring}}, but this one most"
- f" certainly, absolutely {does}."
+ f"We have to remember to escape {braces}." " Like {these}." f" But not {this}."
)
-
-fstring = f"We have to remember to escape {braces}. Like {{these}}. But not {this}."
-
class A:
class B:
def foo():
@@ -364,10 +354,7 @@
def foo():
if not hasattr(module, name):
@ -933,7 +931,7 @@ s = f'Lorem Ipsum is simply dummy text of the printing and typesetting industry:
)
@@ -432,14 +415,12 @@
@@ -432,9 +415,7 @@
assert xxxxxxx_xxxx in [
x.xxxxx.xxxxxx.xxxxx.xxxxxx,
x.xxxxx.xxxxxx.xxxxx.xxxx,
@ -943,15 +941,7 @@ s = f'Lorem Ipsum is simply dummy text of the printing and typesetting industry:
+ ], "xxxxxxxxxxx xxxxxxx xxxx (xxxxxx xxxx) %x xxx xxxxx" % xxxxxxx_xxxx
-value.__dict__[key] = (
- "test" # set some Thrift field to non-None in the struct aa bb cc dd ee
-)
+value.__dict__[
+ key
+] = "test" # set some Thrift field to non-None in the struct aa bb cc dd ee
RE_ONE_BACKSLASH = {
"asdf_hjkl_jkl": re.compile(
value.__dict__[key] = (
@@ -449,8 +430,7 @@
RE_TWO_BACKSLASHES = {
@ -1627,9 +1617,9 @@ class xxxxxxxxxxxxxxxxxxxxx(xxxx.xxxxxxxxxxxxx):
], "xxxxxxxxxxx xxxxxxx xxxx (xxxxxx xxxx) %x xxx xxxxx" % xxxxxxx_xxxx
value.__dict__[
key
] = "test" # set some Thrift field to non-None in the struct aa bb cc dd ee
value.__dict__[key] = (
"test" # set some Thrift field to non-None in the struct aa bb cc dd ee
)
RE_ONE_BACKSLASH = {
"asdf_hjkl_jkl": re.compile(

View file

@ -118,57 +118,7 @@ a = (
```diff
--- Black
+++ Ruff
@@ -1,29 +1,31 @@
-first_item, second_item = (
- some_looooooooong_module.some_looooooooooooooong_function_name(
- first_argument, second_argument, third_argument
- )
+(
+ first_item,
+ second_item,
+) = some_looooooooong_module.some_looooooooooooooong_function_name(
+ first_argument, second_argument, third_argument
)
-some_dict["with_a_long_key"] = (
- some_looooooooong_module.some_looooooooooooooong_function_name(
- first_argument, second_argument, third_argument
- )
+some_dict[
+ "with_a_long_key"
+] = some_looooooooong_module.some_looooooooooooooong_function_name(
+ first_argument, second_argument, third_argument
)
# Make sure it works when the RHS only has one pair of (optional) parens.
-first_item, second_item = (
- some_looooooooong_module.SomeClass.some_looooooooooooooong_variable_name
-)
+(
+ first_item,
+ second_item,
+) = some_looooooooong_module.SomeClass.some_looooooooooooooong_variable_name
-some_dict["with_a_long_key"] = (
- some_looooooooong_module.SomeClass.some_looooooooooooooong_variable_name
-)
+some_dict[
+ "with_a_long_key"
+] = some_looooooooong_module.SomeClass.some_looooooooooooooong_variable_name
# Make sure chaining assignments work.
-first_item, second_item, third_item, forth_item = m["everything"] = (
- some_looooooooong_module.some_looooooooooooooong_function_name(
- first_argument, second_argument, third_argument
- )
+first_item, second_item, third_item, forth_item = m[
+ "everything"
+] = some_looooooooong_module.some_looooooooooooooong_function_name(
+ first_argument, second_argument, third_argument
)
# Make sure when the RHS's first split at the non-optional paren fits,
@@ -60,9 +62,7 @@
@@ -60,9 +60,7 @@
some_arg
).intersection(pk_cols)
@ -179,76 +129,37 @@ a = (
some_kind_of_table[
some_key # type: ignore # noqa: E501
@@ -85,15 +85,29 @@
)
# Multiple targets
-a = b = (
- ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
-)
+a = (
+ b
+) = ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
-a = b = c = d = e = f = g = (
+a = (
+ b
+) = (
+ c
+) = (
+ d
+) = (
+ e
+) = (
+ f
+) = (
+ g
+) = (
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
-) = i = j = (
- kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
-)
+) = (
+ i
+) = (
+ j
+) = kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
a = (
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
```
## Ruff Output
```python
(
first_item,
second_item,
) = some_looooooooong_module.some_looooooooooooooong_function_name(
first_argument, second_argument, third_argument
first_item, second_item = (
some_looooooooong_module.some_looooooooooooooong_function_name(
first_argument, second_argument, third_argument
)
)
some_dict[
"with_a_long_key"
] = some_looooooooong_module.some_looooooooooooooong_function_name(
first_argument, second_argument, third_argument
some_dict["with_a_long_key"] = (
some_looooooooong_module.some_looooooooooooooong_function_name(
first_argument, second_argument, third_argument
)
)
# Make sure it works when the RHS only has one pair of (optional) parens.
(
first_item,
second_item,
) = some_looooooooong_module.SomeClass.some_looooooooooooooong_variable_name
first_item, second_item = (
some_looooooooong_module.SomeClass.some_looooooooooooooong_variable_name
)
some_dict[
"with_a_long_key"
] = some_looooooooong_module.SomeClass.some_looooooooooooooong_variable_name
some_dict["with_a_long_key"] = (
some_looooooooong_module.SomeClass.some_looooooooooooooong_variable_name
)
# Make sure chaining assignments work.
first_item, second_item, third_item, forth_item = m[
"everything"
] = some_looooooooong_module.some_looooooooooooooong_function_name(
first_argument, second_argument, third_argument
first_item, second_item, third_item, forth_item = m["everything"] = (
some_looooooooong_module.some_looooooooooooooong_function_name(
first_argument, second_argument, third_argument
)
)
# Make sure when the RHS's first split at the non-optional paren fits,
@ -308,29 +219,15 @@ some_kind_of_instance.some_kind_of_map[a_key] = (
)
# Multiple targets
a = (
b
) = ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
a = b = (
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
)
a = (
b
) = (
c
) = (
d
) = (
e
) = (
f
) = (
g
) = (
a = b = c = d = e = f = g = (
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
) = (
i
) = (
j
) = kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
) = i = j = (
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
)
a = (
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

View file

@ -421,4 +421,44 @@ def test6():
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -72,13 +72,13 @@
## Breaking left
# Should break `[a]` first
-____[
- a
-] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvv # c
+____[a] = (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvv # c
+)
-____[
- a
-] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvv # cc
+____[a] = (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvv # cc
+)
(
# some weird comments
@@ -136,9 +136,9 @@
# 89 characters parenthesized (collapse)
____a: a = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvv # c
-_a: a[
- b
-] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvv # c
+_a: a[b] = (
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvv # c
+)
## Augmented Assign
```

View file

@ -204,17 +204,17 @@ class RemoveNewlineBeforeClassDocstring:
def f():
"""Black's `Preview.prefer_splitting_right_hand_side_of_assignments`"""
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
] = cccccccc.ccccccccccccc.cccccccc
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
cccccccc.ccccccccccccc.cccccccc
)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
] = cccccccc.ccccccccccccc().cccccccc
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
cccccccc.ccccccccccccc().cccccccc
)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
] = cccccccc.ccccccccccccc(d).cccccccc
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
cccccccc.ccccccccccccc(d).cccccccc
)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (
cccccccc.ccccccccccccc(d).cccccccc + e
@ -228,12 +228,12 @@ def f():
+ eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
)
self._cache: dict[
DependencyCacheKey, list[list[DependencyPackage]]
] = collections.defaultdict(list)
self._cached_dependencies_by_level: dict[
int, list[DependencyCacheKey]
] = collections.defaultdict(list)
self._cache: dict[DependencyCacheKey, list[list[DependencyPackage]]] = (
collections.defaultdict(list)
)
self._cached_dependencies_by_level: dict[int, list[DependencyCacheKey]] = (
collections.defaultdict(list)
)
```

View file

@ -67,4 +67,24 @@ class DefaultRunner:
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -7,9 +7,9 @@
Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb()
)
-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: (
- Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
-) = Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb()
+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = (
+ Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb()
+)
JSONSerializable: TypeAlias = (
"str | int | float | bool | None | list | tuple | JSONMapping"
```

View file

@ -169,4 +169,40 @@ c = b[dddddd, aaaaaa] = (
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -1,7 +1,5 @@
# break left hand side
-a1akjdshflkjahdslkfjlasfdahjlfds = (
- bakjdshflkjahdslkfjlasfdahjlfds
-) = (
+a1akjdshflkjahdslkfjlasfdahjlfds = bakjdshflkjahdslkfjlasfdahjlfds = (
cakjdshflkjahdslkfjlasfdahjlfds
) = kjaödkjaföjfahlfdalfhaöfaöfhaöfha = fkjaödkjaföjfahlfdalfhaöfaöfhaöfha = g = 3
@@ -9,15 +7,13 @@
a2 = b2 = 2
# Break the last element
-a = (
- asdf
-) = (
+a = asdf = (
fjhalsdljfalflaflapamsakjsdhflakjdslfjhalsdljfalflaflapamsakjsdhflakjdslfjhalsdljfal
) = 1
-aa = [
- bakjdshflkjahdslkfjlasfdahjlfds
-] = dddd = ddd = fkjaödkjaföjfahlfdalfhaöfaöfhaöfha = g = [3]
+aa = [bakjdshflkjahdslkfjlasfdahjlfds] = dddd = ddd = (
+ fkjaödkjaföjfahlfdalfhaöfaöfhaöfha
+) = g = [3]
aa = [] = dddd = ddd = fkjaödkjaföjfahlfdalfhaöfaöfhaöfha = g = [3]
```

View file

@ -0,0 +1,457 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/assignment_split_value_first.py
---
## Input
```python
#######
# Unsplittable target and value
# Only parenthesize the value if it makes it fit, otherwise avoid parentheses.
b = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvee
bbbbbbbbbbbbbbbb = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvv
# Avoid parenthesizing the value even if the target exceeds the configured width
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = bbb
############
# Splittable targets
# Does not double-parenthesize tuples
(
first_item,
second_item,
) = some_looooooooong_module.some_loooooog_function_name(
first_argument, second_argument, third_argument
)
# Preserve parentheses around the first target
(
req["ticket"]["steps"]["step"][0]["tasks"]["task"]["fields"]["field"][
"access_request"
]["destinations"]["destination"][0]["ip_address"]
) = dst
# Augmented assignment
req["ticket"]["steps"]["step"][0]["tasks"]["task"]["fields"]["field"][
"access_request"
] += dst
# Always parenthesize the value if it avoids splitting the target, regardless of the value's width.
_a: a[aaaa] = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvv
)
#####
# Avoid parenthesizing the value if the expression right before the `=` splits to avoid an unnecessary pair of parentheses
# The type annotation is guaranteed to split because it is too long.
_a: a[
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvv
] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvv
# The target is too long
(
aaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvv
# The target splits because of a magic trailing comma
(
a,
b,
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvvvv
# The targets split because of a comment
(
# leading
a
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvvvv
(
a
# trailing
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvvvv
(
a, # nested
b
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvvvv
#######
# Multi targets
# Black always parenthesizes the right if using multiple targets regardless if the parenthesized value exceeds the
# the configured line width or not
aaaa = bbbbbbbbbbbbbbbb = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvee
)
# Black does parenthesize the target if the target itself exceeds the line width and only parenthesizes
# the values if it makes it fit.
# The second target is too long to ever fit into the configured line width.
aaaa = (
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbdddd
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvee
# Does also apply for other multi target assignments, as soon as a single target exceeds the configured
# width
aaaaaa = a["aaa"] = bbbbb[aa, bbb, cccc] = dddddddddd = eeeeee = (
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
######################
# Call expressions:
# For unsplittable targets: Parenthesize the call expression if it makes it fit.
#
# For splittable targets:
# Only parenthesize a call expression if the parens of the call don't fit on the same line
# as the target. Don't parenthesize the call expression if the target (or annotation) right before
# splits.
# Don't parenthesize the function call if the left is unsplittable.
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = a.b.function(
arg1, arg2, arg3
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
[1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
[1, 2, 3],
arg1,
[1, 2, 3],
arg2,
[1, 2, 3],
arg3,
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd,
eeeeeeeeeeeeee,
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = (
function()
)
this_is_a_ridiculously_long_name_and_nobodyddddddddddddddddddddddddddddddd = (
a.b.function(arg1, arg2, arg3)
)
this_is_a_ridiculously_long_name_and_nobodyddddddddddddddddddddddddddddddd = function()
this_is_a_ridiculously_long_name_and_nobodyddddddddddddddddddddddddddddddd = function(
[1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3
)
this_is_a_ridiculously_long_name_and_nobodyddddddddddddddddddddddddddddddd = function(
[1, 2, 3],
arg1,
[1, 2, 3],
arg2,
[1, 2, 3],
arg3,
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd,
eeeeeeeeeeeeee,
)
####### Fluent call expressions
# Uses the regular `Multiline` layout where the entire `value` gets parenthesized
# if it doesn't fit on the line.
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use = (
function().b().c([1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3)
)
#######
# Test comment inlining
value.__dict__[key] = (
"test" # set some Thrift field to non-None in the struct aa bb cc dd ee
)
value.__dict__.keye = (
"test" # set some Thrift field to non-None in the struct aa bb cc dd ee
)
value.__dict__.keye = (
"test" # set some Thrift field to non-None in the struct aa bb cc dd ee
)
# Don't parenthesize the value because the target's trailing comma forces it to split.
a[
aaaaaaa,
b,
] = cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc # comment
# Parenthesize the value, but don't duplicate the comment.
a[aaaaaaa, b] = (
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc # comment
)
# Format both as flat, but don't loos the comment.
a[aaaaaaa, b] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb # comment
#######################################################
# Test the case where a parenthesized value now fits:
a[
aaaaaaa,
b
] = (
cccccccc # comment
)
# Splits the target but not the value because of the magic trailing comma.
a[
aaaaaaa,
b,
] = (
cccccccc # comment
)
# Splits the second target because of the comment and the first target because of the trailing comma.
a[
aaaaaaa,
b,
] = (
# leading comment
b
) = (
cccccccc # comment
)
########
# Type Alias Statement
type A[str, int, number] = VeryLongTypeNameThatShouldBreakFirstToTheRightBeforeSplitngtin
type A[VeryLongTypeNameThatShouldBreakFirstToTheRightBeforeSplitngtinthatExceedsTheWidth] = str
```
## Outputs
### Output 1
```
indent-style = space
line-width = 88
indent-width = 4
quote-style = Double
line-ending = LineFeed
magic-trailing-comma = Respect
docstring-code = Disabled
docstring-code-line-width = 88
preview = Enabled
```
```python
#######
# Unsplittable target and value
# Only parenthesize the value if it makes it fit, otherwise avoid parentheses.
b = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvee
bbbbbbbbbbbbbbbb = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvv
)
# Avoid parenthesizing the value even if the target exceeds the configured width
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = bbb
############
# Splittable targets
# Does not double-parenthesize tuples
(
first_item,
second_item,
) = some_looooooooong_module.some_loooooog_function_name(
first_argument, second_argument, third_argument
)
# Preserve parentheses around the first target
(
req["ticket"]["steps"]["step"][0]["tasks"]["task"]["fields"]["field"][
"access_request"
]["destinations"]["destination"][0]["ip_address"]
) = dst
# Augmented assignment
req["ticket"]["steps"]["step"][0]["tasks"]["task"]["fields"]["field"][
"access_request"
] += dst
# Always parenthesize the value if it avoids splitting the target, regardless of the value's width.
_a: a[aaaa] = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvv
)
#####
# Avoid parenthesizing the value if the expression right before the `=` splits to avoid an unnecessary pair of parentheses
# The type annotation is guaranteed to split because it is too long.
_a: a[
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvv
] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvv
# The target is too long
(
aaaaaaaaaaa,
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvv
# The target splits because of a magic trailing comma
(
a,
b,
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvvvv
# The targets split because of a comment
(
# leading
a
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvvvv
(
a
# trailing
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvvvv
(
a, # nested
b,
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvvvv
#######
# Multi targets
# Black always parenthesizes the right if using multiple targets regardless if the parenthesized value exceeds the
# the configured line width or not
aaaa = bbbbbbbbbbbbbbbb = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvee
)
# Black does parenthesize the target if the target itself exceeds the line width and only parenthesizes
# the values if it makes it fit.
# The second target is too long to ever fit into the configured line width.
aaaa = (
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbdddd
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbvvvvvvvvvvvvvvvvvee
# Does also apply for other multi target assignments, as soon as a single target exceeds the configured
# width
aaaaaa = a["aaa"] = bbbbb[aa, bbb, cccc] = dddddddddd = eeeeee = (
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
) = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
######################
# Call expressions:
# For unsplittable targets: Parenthesize the call expression if it makes it fit.
#
# For splittable targets:
# Only parenthesize a call expression if the parens of the call don't fit on the same line
# as the target. Don't parenthesize the call expression if the target (or annotation) right before
# splits.
# Don't parenthesize the function call if the left is unsplittable.
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = a.b.function(
arg1, arg2, arg3
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
[1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function(
[1, 2, 3],
arg1,
[1, 2, 3],
arg2,
[1, 2, 3],
arg3,
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd,
eeeeeeeeeeeeee,
)
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use_one_like_it = function()
this_is_a_ridiculously_long_name_and_nobodyddddddddddddddddddddddddddddddd = (
a.b.function(arg1, arg2, arg3)
)
this_is_a_ridiculously_long_name_and_nobodyddddddddddddddddddddddddddddddd = function()
this_is_a_ridiculously_long_name_and_nobodyddddddddddddddddddddddddddddddd = function(
[1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3
)
this_is_a_ridiculously_long_name_and_nobodyddddddddddddddddddddddddddddddd = function(
[1, 2, 3],
arg1,
[1, 2, 3],
arg2,
[1, 2, 3],
arg3,
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd,
eeeeeeeeeeeeee,
)
####### Fluent call expressions
# Uses the regular `Multiline` layout where the entire `value` gets parenthesized
# if it doesn't fit on the line.
this_is_a_ridiculously_long_name_and_nobody_in_their_right_mind_would_use = (
function().b().c([1, 2, 3], arg1, [1, 2, 3], arg2, [1, 2, 3], arg3)
)
#######
# Test comment inlining
value.__dict__[key] = (
"test" # set some Thrift field to non-None in the struct aa bb cc dd ee
)
value.__dict__.keye = (
"test" # set some Thrift field to non-None in the struct aa bb cc dd ee
)
value.__dict__.keye = (
"test" # set some Thrift field to non-None in the struct aa bb cc dd ee
)
# Don't parenthesize the value because the target's trailing comma forces it to split.
a[
aaaaaaa,
b,
] = cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc # comment
# Parenthesize the value, but don't duplicate the comment.
a[aaaaaaa, b] = (
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc # comment
)
# Format both as flat, but don't loos the comment.
a[aaaaaaa, b] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb # comment
#######################################################
# Test the case where a parenthesized value now fits:
a[aaaaaaa, b] = cccccccc # comment
# Splits the target but not the value because of the magic trailing comma.
a[
aaaaaaa,
b,
] = cccccccc # comment
# Splits the second target because of the comment and the first target because of the trailing comma.
a[
aaaaaaa,
b,
] = (
# leading comment
b
) = cccccccc # comment
########
# Type Alias Statement
type A[str, int, number] = (
VeryLongTypeNameThatShouldBreakFirstToTheRightBeforeSplitngtin
)
type A[
VeryLongTypeNameThatShouldBreakFirstToTheRightBeforeSplitngtinthatExceedsTheWidth
] = str
```