Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

<!-- Changes that affect Black's preview style -->

- Split method chains consistently when a standalone comment appears between chained
calls (#5218)
- Fix unnecessary parentheses around short RHS expressions in indexed assignments like
`x[key] = expr` (#5095)
- Parenthesize tuple expressions in `yield` statements for consistency with function
Expand Down
2 changes: 2 additions & 0 deletions docs/the_black_code_style/future_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Currently, the following features are included in the preview style:
anyway; let the bracket explode instead. ([see below](labels/hug-comparator))
- `parenthesize_tuple_in_yield`: Add parentheses around tuple expressions in `yield`
statements.
- `split_method_chain_on_standalone_comment`: Split method chains consistently when a
standalone comment appears between chained calls.

(labels/wrap-comprehension-in)=

Expand Down
4 changes: 4 additions & 0 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,10 @@ def delimiter_split(
if (
delimiter_priority == DOT_PRIORITY
and bt.delimiter_count_with_priority(delimiter_priority) == 1
and not (
Preview.split_method_chain_on_standalone_comment in mode
and line.contains_standalone_comments()
)
):
raise CannotSplit("Splitting a single attribute from its owner looks wrong")

Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class Preview(Enum):
pyi_blank_line_after_function_docstring = auto()
hug_comparator = auto()
parenthesize_tuple_in_yield = auto()
split_method_chain_on_standalone_comment = auto()


UNSTABLE_FEATURES: set[Preview] = {
Expand Down
3 changes: 2 additions & 1 deletion src/black/resources/black.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"pyi_blank_line_before_decorated_class",
"pyi_blank_line_after_function_docstring",
"hug_comparator",
"parenthesize_tuple_in_yield"
"parenthesize_tuple_in_yield",
"split_method_chain_on_standalone_comment"
]
},
"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."
Expand Down
30 changes: 30 additions & 0 deletions tests/data/cases/preview_method_chain_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# flags: --preview
x = (
very_long_function_name_with_many_arguments(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000).f()
# Comment
.g()
)
y = (
very_long_function_name_with_many_arguments(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000).f()
# Comment
.g()
.h()
)
# output
x = (
very_long_function_name_with_many_arguments(
100, 200, 300, 400, 500, 600, 700, 800, 900, 1000
)
.f()
# Comment
.g()
)
y = (
very_long_function_name_with_many_arguments(
100, 200, 300, 400, 500, 600, 700, 800, 900, 1000
)
.f()
# Comment
.g()
.h()
)