diff --git a/crates/ruff_python_formatter/src/expression/expr_attribute.rs b/crates/ruff_python_formatter/src/expression/expr_attribute.rs index 36d2f6da12..6e499d6547 100644 --- a/crates/ruff_python_formatter/src/expression/expr_attribute.rs +++ b/crates/ruff_python_formatter/src/expression/expr_attribute.rs @@ -199,7 +199,6 @@ impl NeedsParentheses for ExprAttribute { self.into(), context.comments().ranges(), context.source(), - context ), CallChainLayout::Fluent(_) ) { diff --git a/crates/ruff_python_formatter/src/expression/expr_call.rs b/crates/ruff_python_formatter/src/expression/expr_call.rs index 609e5357e4..deaad509ec 100644 --- a/crates/ruff_python_formatter/src/expression/expr_call.rs +++ b/crates/ruff_python_formatter/src/expression/expr_call.rs @@ -91,7 +91,6 @@ impl NeedsParentheses for ExprCall { self.into(), context.comments().ranges(), context.source(), - context ), CallChainLayout::Fluent(_) ) { diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index a61ffd0205..95a326d5e3 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -96,7 +96,6 @@ impl NeedsParentheses for ExprSubscript { self.into(), context.comments().ranges(), context.source(), - context ), CallChainLayout::Fluent(_) ) { diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 9f88b70db8..c32407f490 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -19,9 +19,7 @@ use crate::expression::parentheses::{ optional_parentheses, parenthesized, }; use crate::prelude::*; -use crate::preview::{ - is_fluent_layout_more_often_enabled, is_hug_parens_with_braces_and_square_brackets_enabled, -}; +use crate::preview::is_hug_parens_with_braces_and_square_brackets_enabled; mod binary_like; pub(crate) mod expr_attribute; @@ -963,9 +961,6 @@ impl CallChainLayout { mut expr: ExprRef, comment_ranges: &CommentRanges, source: &str, - // This can be deleted once the preview style - // is stabilized - context: &PyFormatContext, ) -> Self { // Count of attribute values which are called or // subscripted, after the leftmost parenthesized @@ -1039,13 +1034,7 @@ impl CallChainLayout { } } - let threshold = if is_fluent_layout_more_often_enabled(context) { - call_like_count + u32::from(root_value_parenthesized) - } else { - computed_attribute_values_after_parentheses + u32::from(root_value_parenthesized) - }; - - if threshold < 2 { + if computed_attribute_values_after_parentheses + u32::from(root_value_parenthesized) < 2 { CallChainLayout::NonFluent } else { CallChainLayout::Fluent(AttributeState::CallsOrSubscriptsPreceding(call_like_count)) @@ -1066,7 +1055,6 @@ impl CallChainLayout { item.into(), f.context().comments().ranges(), f.context().source(), - f.context(), ) } else { CallChainLayout::NonFluent diff --git a/crates/ruff_python_formatter/src/preview.rs b/crates/ruff_python_formatter/src/preview.rs index a8cd856633..9995c4c480 100644 --- a/crates/ruff_python_formatter/src/preview.rs +++ b/crates/ruff_python_formatter/src/preview.rs @@ -66,10 +66,3 @@ pub(crate) const fn is_parenthesize_lambda_bodies_enabled(context: &PyFormatCont pub(crate) const fn is_fluent_layout_split_first_call_enabled(context: &PyFormatContext) -> bool { context.is_preview() } - -/// Returns `true` if the -/// [`fluent_layout_more_often`](https://github.com/astral-sh/ruff/pull/21369) preview -/// style is enabled. -pub(crate) const fn is_fluent_layout_more_often_enabled(context: &PyFormatContext) -> bool { - context.is_preview() -} diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_long_dict_values.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_long_dict_values.py.snap index 5efb50eefa..f021bad61c 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_long_dict_values.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_long_dict_values.py.snap @@ -228,7 +228,7 @@ class Random: } { -@@ -139,17 +133,16 @@ +@@ -139,17 +133,17 @@ class Random: def func(): @@ -244,8 +244,9 @@ class Random: - "actionTimestamp": ( - Timestamp(seconds=1530584000, nanos=0).ToJsonString() - ), -+ "actionTimestamp": Timestamp(seconds=1530584000, nanos=0) -+ .ToJsonString(), ++ "actionTimestamp": Timestamp( ++ seconds=1530584000, nanos=0 ++ ).ToJsonString(), } }, - } @@ -398,8 +399,9 @@ class Random: "timestamp": 1234, "latitude": 1, "longitude": 2, - "actionTimestamp": Timestamp(seconds=1530584000, nanos=0) - .ToJsonString(), + "actionTimestamp": Timestamp( + seconds=1530584000, nanos=0 + ).ToJsonString(), } }, }) diff --git a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_multiline_strings.py.snap b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_multiline_strings.py.snap index 5507caf173..29af6e1b07 100644 --- a/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_multiline_strings.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/black_compatibility@cases__preview_multiline_strings.py.snap @@ -414,12 +414,12 @@ a = b if """ + """cow +moos""" +} - [ - """cow -+moos""" -+] +[ + """cow ++moos""" ++] + [ + """cow moos""", @@ -206,7 +245,9 @@ "c" diff --git a/crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap index 14a2a4289c..628910f153 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@docstring_code_examples_dynamic_line_width.py.snap @@ -2914,19 +2914,6 @@ def length_rst_in_section(): ```diff --- Stable +++ Preview -@@ -683,9 +683,9 @@ - ... self._df.filter(pl.col(col).str.starts_with(c)) - ... for c in sorted( - ... set( -- ... df.select( -- ... pl.col(col).str.slice(0, 1) -- ... ).to_series() -+ ... df -+ ... .select(pl.col(col).str.slice(0, 1)) -+ ... .to_series() - ... ) - ... ) - ... ] @@ -700,9 +700,11 @@ Examples diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__attribute.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__attribute.py.snap index d7eb98968f..4677ca06de 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__attribute.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__attribute.py.snap @@ -429,20 +429,3 @@ variable = ( .first_method("some string") ) ``` - - -## Preview changes -```diff ---- Stable -+++ Preview -@@ -171,7 +171,8 @@ - if ( - ( - something # a comment -- ).first_method("some string") # second comment -+ ) -+ .first_method("some string") # second comment - ): - pass - -``` diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__split_empty_brackets.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__split_empty_brackets.py.snap index 2f8c9f503e..ccb7c95aa6 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__split_empty_brackets.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__split_empty_brackets.py.snap @@ -198,24 +198,7 @@ response = await sync_to_async( ```diff --- Stable +++ Preview -@@ -18,10 +18,14 @@ - ).casefold(1) - - ct_match = ( -- unicodedata.normalize("NFKC", s1).casefold( -+ unicodedata -+ .normalize("NFKC", s1) -+ .casefold( - # foo - ) -- == unicodedata.normalize("NFKCNFKCNFKCNFKCNFKC", s2).casefold( -+ == unicodedata -+ .normalize("NFKCNFKCNFKCNFKCNFKC", s2) -+ .casefold( - # foo - ) - ) -@@ -62,9 +66,9 @@ +@@ -62,9 +62,9 @@ 1 }.unicodedata.normalize("NFKCNFKCNFKCNFKCNFKC", s2).casefold() diff --git a/crates/ruff_python_formatter/tests/snapshots/format@parentheses__call_chains.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@parentheses__call_chains.py.snap index aa50015b27..8cfc5fbd1a 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@parentheses__call_chains.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@parentheses__call_chains.py.snap @@ -472,26 +472,7 @@ max_message_id = ( ```diff --- Stable +++ Preview -@@ -4,11 +4,13 @@ - aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa - ).a(aaaa) - --raise OsError( -- "sökdjffffsldkfjlhsakfjhalsökafhsöfdahsödfjösaaksjdllllllllllllll" --) from a.aaaaa( -- aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa --).a(aaaa) -+raise OsError("sökdjffffsldkfjlhsakfjhalsökafhsöfdahsödfjösaaksjdllllllllllllll") from ( -+ a -+ .aaaaa( -+ aksjdhflsakhdflkjsadlfajkslhfdkjsaldajlahflashdfljahlfksajlhfajfjfsaahflakjslhdfkjalhdskjfa -+ ) -+ .a(aaaa) -+) - - a1 = Blog.objects.filter(entry__headline__contains="Lennon").filter( - entry__pub_date__year=2008 -@@ -21,7 +23,8 @@ +@@ -21,7 +21,8 @@ ) raise OsError("") from ( @@ -501,7 +482,7 @@ max_message_id = ( entry__headline__contains="Lennon", ) .filter( -@@ -33,7 +36,8 @@ +@@ -33,7 +34,8 @@ ) raise OsError("sökdjffffsldkfjlhsakfjhalsökafhsöfdahsödfjösaaksjdllllllllllllll") from ( @@ -511,7 +492,7 @@ max_message_id = ( entry__headline__contains="Lennon", ) .filter( -@@ -46,7 +50,8 @@ +@@ -46,7 +48,8 @@ # Break only after calls and indexing b1 = ( @@ -521,7 +502,7 @@ max_message_id = ( .filter( models.Customer.account_id == account_id, models.Customer.email == email_address ) -@@ -54,7 +59,8 @@ +@@ -54,7 +57,8 @@ ) b2 = ( @@ -531,17 +512,8 @@ max_message_id = ( entry__headline__contains="Lennon", ) .limit_results[:10] -@@ -65,12 +71,15 @@ - - # Nested call chains - c1 = ( -- Blog.objects.filter( -+ Blog.objects -+ .filter( - entry__headline__contains="Lennon", -- ).filter( -+ ) -+ .filter( +@@ -70,7 +74,8 @@ + ).filter( entry__pub_date__year=2008, ) - + Blog.objects.filter( @@ -550,7 +522,7 @@ max_message_id = ( entry__headline__contains="McCartney", ) .limit_results[:10] -@@ -89,19 +98,23 @@ +@@ -89,7 +94,8 @@ d11 = x.e().e().e() # d12 = x.e().e().e() # d13 = ( @@ -560,14 +532,7 @@ max_message_id = ( .e() .e() ) - - # Doesn't fit, default - d2 = ( -- x.e().esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() # -+ x -+ .e() -+ .esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkfsdddd() # - ) +@@ -101,7 +107,8 @@ # Doesn't fit, fluent style d3 = ( @@ -577,32 +542,7 @@ max_message_id = ( .esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk() .esadjkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk() ) -@@ -199,16 +212,21 @@ - - ( - ( -- df1_aaaaaaaaaaaa.merge().groupby( -+ df1_aaaaaaaaaaaa -+ .merge() -+ .groupby( - 1, - ) -- ).sum() -+ ) -+ .sum() - ) - - - ( - ( -- df1_aaaaaaaaaaaa.merge().groupby( -+ df1_aaaaaaaaaaaa -+ .merge() -+ .groupby( - 1, - ) - ) -@@ -218,17 +236,20 @@ +@@ -218,7 +225,8 @@ ( ( @@ -612,11 +552,7 @@ max_message_id = ( .groupby( 1, ) - .bar() -- ).sum() -+ ) -+ .sum() - ) +@@ -228,7 +236,8 @@ ( ( diff --git a/docs/formatter.md b/docs/formatter.md index e8c199d9c4..f4c39edabb 100644 --- a/docs/formatter.md +++ b/docs/formatter.md @@ -538,33 +538,10 @@ x = ( Both the stable and preview formatting are variants of something called a **fluent layout**. -In general, this preview style differs from the stable style in the -following two ways: - -1. In the case where a fluent layout is applied, the preview formatting -differs from the stable formatting only at the first attribute that precedes +In general, this preview style differs from the stable style +only at the first attribute that precedes a call or subscript. The preview formatting breaks _before_ this attribute, while the stable formatting breaks _after_ the call or subscript. -2. When `preview` is enabled, the fluent layout will be used slightly more often, as we explain below. - -To explain point (2), we need to explain the heuristic used for deciding -whether to apply a fluent layout. In stable, the heuristic is essentially -to apply fluent formatting in the following situations: - -- A parenthesized, call, or subscript expression is followed by at least one attribute that is called or subscripted, excluding the last attribute in the chain, or -- A non-attribute expression is followed by at least two attributes that -are called or subscripted, excluding the last attribute in the chain - -In `preview` the heuristic is identical except that we do not exclude the -last attribute in the chain from consideration. - -For example, with `line-length = 8`, we have: - -```python -x = a[0].b().c # formatted to fluent with and without preview -x = a.b().c().d # formatted to fluent with and without preview -x = a.d.b().c() # formatted to fluent only in preview -``` ## Sorting imports