Skip to content

Commit cd5571e

Browse files
IshaanIshaan
authored andcommitted
Fix preview split for commented method chains
1 parent c4c9a93 commit cd5571e

5 files changed

Lines changed: 39 additions & 1 deletion

File tree

docs/the_black_code_style/future_style.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Currently, the following features are included in the preview style:
4343
anyway; let the bracket explode instead. ([see below](labels/hug-comparator))
4444
- `parenthesize_tuple_in_yield`: Add parentheses around tuple expressions in `yield`
4545
statements.
46+
- `split_method_chain_on_standalone_comment`: Split method chains consistently when a
47+
standalone comment appears between chained calls.
4648

4749
(labels/wrap-comprehension-in)=
4850

src/black/linegen.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,10 @@ def delimiter_split(
14381438
if (
14391439
delimiter_priority == DOT_PRIORITY
14401440
and bt.delimiter_count_with_priority(delimiter_priority) == 1
1441+
and not (
1442+
Preview.split_method_chain_on_standalone_comment in mode
1443+
and line.contains_standalone_comments()
1444+
)
14411445
):
14421446
raise CannotSplit("Splitting a single attribute from its owner looks wrong")
14431447

src/black/mode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ class Preview(Enum):
263263
pyi_blank_line_after_function_docstring = auto()
264264
hug_comparator = auto()
265265
parenthesize_tuple_in_yield = auto()
266+
split_method_chain_on_standalone_comment = auto()
266267

267268

268269
UNSTABLE_FEATURES: set[Preview] = {

src/black/resources/black.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292
"pyi_blank_line_before_decorated_class",
9393
"pyi_blank_line_after_function_docstring",
9494
"hug_comparator",
95-
"parenthesize_tuple_in_yield"
95+
"parenthesize_tuple_in_yield",
96+
"split_method_chain_on_standalone_comment"
9697
]
9798
},
9899
"description": "Enable specific features included in the `--unstable` style. Requires `--preview`. No compatibility guarantees are provided on the behavior or existence of any unstable features."
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# flags: --preview
2+
x = (
3+
very_long_function_name_with_many_arguments(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000).f()
4+
# Comment
5+
.g()
6+
)
7+
y = (
8+
very_long_function_name_with_many_arguments(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000).f()
9+
# Comment
10+
.g()
11+
.h()
12+
)
13+
# output
14+
x = (
15+
very_long_function_name_with_many_arguments(
16+
100, 200, 300, 400, 500, 600, 700, 800, 900, 1000
17+
)
18+
.f()
19+
# Comment
20+
.g()
21+
)
22+
y = (
23+
very_long_function_name_with_many_arguments(
24+
100, 200, 300, 400, 500, 600, 700, 800, 900, 1000
25+
)
26+
.f()
27+
# Comment
28+
.g()
29+
.h()
30+
)

0 commit comments

Comments
 (0)