From e54ab69f5902c9d10f5d1cc88698c1c5fc35eb79 Mon Sep 17 00:00:00 2001 From: Ishaan Date: Fri, 3 Jul 2026 00:52:25 +0530 Subject: [PATCH] Fix preview split for commented method chains --- CHANGES.md | 2 ++ docs/the_black_code_style/future_style.md | 2 ++ src/black/linegen.py | 4 +++ src/black/mode.py | 1 + src/black/resources/black.schema.json | 3 +- .../cases/preview_method_chain_comment.py | 30 +++++++++++++++++++ 6 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/data/cases/preview_method_chain_comment.py diff --git a/CHANGES.md b/CHANGES.md index 8a4c7456a6a..c847cf8ed03 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -34,6 +34,8 @@ +- 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 diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index 00cd4af4aeb..cda7f8e62da 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -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)= diff --git a/src/black/linegen.py b/src/black/linegen.py index c9db9d8b4e9..259faa0f8e4 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -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") diff --git a/src/black/mode.py b/src/black/mode.py index 9646f218dc4..69d6b11f890 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -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] = { diff --git a/src/black/resources/black.schema.json b/src/black/resources/black.schema.json index f467f1a6879..144b9e71db9 100644 --- a/src/black/resources/black.schema.json +++ b/src/black/resources/black.schema.json @@ -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." diff --git a/tests/data/cases/preview_method_chain_comment.py b/tests/data/cases/preview_method_chain_comment.py new file mode 100644 index 00000000000..0a929c9a587 --- /dev/null +++ b/tests/data/cases/preview_method_chain_comment.py @@ -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() +)