Skip to content

Commit dcadaf2

Browse files
committed
Keep lambda parameters on a single line
Summary -- This PR changes our formatting of `lambda` expressions to keep the parameters on a single line, at least if there are no comments. This fixes #8179. Black formatting and this PR's formatting: ```py def a(): return b( c, d, e, f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( *args, **kwargs ), ) ``` Stable Ruff formatting ```py def a(): return b( c, d, e, f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), ) ``` I split this off from #21385 because it seemed like the simpler change and helpful to isolate from the body parenthesization ecosystem and performance changes. However, as Micha pointed out, we need the formatting from #21385 to land first, so this branch is currently stacked on that one. Test Plan -- New formatting on tests from #8465 and #21385
1 parent b441ac6 commit dcadaf2

3 files changed

Lines changed: 77 additions & 8 deletions

File tree

crates/ruff_python_formatter/src/expression/expr_lambda.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use ruff_formatter::RemoveSoftLinesBuffer;
12
use ruff_formatter::write;
23
use ruff_python_ast::AnyNodeRef;
34
use ruff_python_ast::ExprLambda;
@@ -10,6 +11,7 @@ use crate::expression::parentheses::{
1011
};
1112
use crate::other::parameters::ParametersParentheses;
1213
use crate::prelude::*;
14+
use crate::preview::is_force_single_line_lambda_parameters_enabled;
1315
use crate::preview::is_parenthesize_lambda_bodies_enabled;
1416

1517
#[derive(Default)]
@@ -41,12 +43,25 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
4143
write!(f, [dangling_comments(dangling_before_parameters)])?;
4244
}
4345

44-
write!(
45-
f,
46-
[parameters
47-
.format()
48-
.with_options(ParametersParentheses::Never)]
49-
)?;
46+
// Try to keep the parameters on a single line, unless there are intervening comments.
47+
if is_force_single_line_lambda_parameters_enabled(f.context())
48+
&& !comments.contains_comments(parameters.as_ref().into())
49+
{
50+
let mut buffer = RemoveSoftLinesBuffer::new(f);
51+
write!(
52+
buffer,
53+
[parameters
54+
.format()
55+
.with_options(ParametersParentheses::Never)]
56+
)?;
57+
} else {
58+
write!(
59+
f,
60+
[parameters
61+
.format()
62+
.with_options(ParametersParentheses::Never)]
63+
)?;
64+
}
5065

5166
write!(f, [token(":")])?;
5267

crates/ruff_python_formatter/src/preview.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,12 @@ pub(crate) const fn is_avoid_parens_for_long_as_captures_enabled(
5959
pub(crate) const fn is_parenthesize_lambda_bodies_enabled(context: &PyFormatContext) -> bool {
6060
context.is_preview()
6161
}
62+
63+
/// Returns `true` if the
64+
/// [`force_single_line_lambda_parameters`](https://github.com/astral-sh/ruff/pull/21385) preview
65+
/// style is enabled.
66+
pub(crate) const fn is_force_single_line_lambda_parameters_enabled(
67+
context: &PyFormatContext,
68+
) -> bool {
69+
context.is_preview()
70+
}

crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,52 @@ msg = (
757757
) # Trailing
758758
759759
760-
@@ -320,9 +324,9 @@
760+
@@ -280,9 +284,9 @@
761+
] # Trailing
762+
# Trailing
763+
764+
-lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
765+
- *args, **kwargs
766+
-), e=1, f=2, g=2: d
767+
+lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: (
768+
+ d
769+
+)
770+
771+
772+
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
773+
@@ -291,9 +295,9 @@
774+
c,
775+
d,
776+
e,
777+
- f=lambda self,
778+
- *args,
779+
- **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs),
780+
+ f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
781+
+ *args, **kwargs
782+
+ ),
783+
)
784+
785+
786+
@@ -302,15 +306,9 @@
787+
c,
788+
d,
789+
e,
790+
- f=lambda self,
791+
- araa,
792+
- kkkwargs,
793+
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
794+
- args,
795+
- kwargs,
796+
- e=1,
797+
- f=2,
798+
- g=2: d,
799+
+ f=lambda self, araa, kkkwargs, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, args, kwargs, e=1, f=2, g=2: (
800+
+ d
801+
+ ),
802+
g=10,
803+
)
804+
805+
@@ -320,9 +318,9 @@
761806
c,
762807
d,
763808
e,
@@ -770,7 +815,7 @@ msg = (
770815
)
771816
772817
773-
@@ -337,6 +341,6 @@
818+
@@ -337,6 +335,6 @@
774819
775820
776821
# temporary copy of a test from preview_long_strings.py

0 commit comments

Comments
 (0)