keep lambda parameters on a single line

This commit is contained in:
Brent Westbrook 2025-11-20 15:09:00 -05:00
parent 1e70c991a2
commit 8cc884428d
No known key found for this signature in database
3 changed files with 92 additions and 7 deletions

View File

@ -1,4 +1,4 @@
use ruff_formatter::write; use ruff_formatter::{RemoveSoftLinesBuffer, write};
use ruff_python_ast::AnyNodeRef; use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprLambda; use ruff_python_ast::ExprLambda;
use ruff_text_size::Ranged; use ruff_text_size::Ranged;
@ -7,6 +7,7 @@ use crate::comments::dangling_comments;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses}; use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::other::parameters::ParametersParentheses; use crate::other::parameters::ParametersParentheses;
use crate::prelude::*; use crate::prelude::*;
use crate::preview::is_force_single_line_lambda_parameters_enabled;
#[derive(Default)] #[derive(Default)]
pub struct FormatExprLambda; pub struct FormatExprLambda;
@ -37,12 +38,25 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
write!(f, [dangling_comments(dangling_before_parameters)])?; write!(f, [dangling_comments(dangling_before_parameters)])?;
} }
write!( // Try to keep the parameters on a single line, unless there are intervening comments.
f, if is_force_single_line_lambda_parameters_enabled(f.context())
[parameters && !comments.contains_comments(parameters.as_ref().into())
.format() {
.with_options(ParametersParentheses::Never)] let mut buffer = RemoveSoftLinesBuffer::new(f);
)?; write!(
buffer,
[parameters
.format()
.with_options(ParametersParentheses::Never)]
)?;
} else {
write!(
f,
[parameters
.format()
.with_options(ParametersParentheses::Never)]
)?;
}
write!(f, [token(":")])?; write!(f, [token(":")])?;

View File

@ -52,3 +52,12 @@ pub(crate) const fn is_avoid_parens_for_long_as_captures_enabled(
) -> bool { ) -> bool {
context.is_preview() context.is_preview()
} }
/// Returns `true` if the
/// [`force_single_line_lambda_parameters`](https://github.com/astral-sh/ruff/pull/21385) preview
/// style is enabled.
pub(crate) const fn is_force_single_line_lambda_parameters_enabled(
context: &PyFormatContext,
) -> bool {
context.is_preview()
}

View File

@ -676,3 +676,65 @@ class C:
else storage.Bucket(mock_service, destination_bucket_name) else storage.Bucket(mock_service, destination_bucket_name)
) )
``` ```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -280,9 +280,7 @@
] # Trailing
# Trailing
-lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
- *args, **kwargs
-), e=1, f=2, g=2: d
+lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
@@ -291,9 +289,9 @@
c,
d,
e,
- f=lambda self,
- *args,
- **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs),
+ f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
+ *args, **kwargs
+ ),
)
@@ -302,15 +300,7 @@
c,
d,
e,
- f=lambda self,
- araa,
- kkkwargs,
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
- args,
- kwargs,
- e=1,
- f=2,
- g=2: d,
+ f=lambda self, araa, kkkwargs, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, args, kwargs, e=1, f=2, g=2: d,
g=10,
)
@@ -320,9 +310,10 @@
c,
d,
e,
- f=lambda self,
- *args,
- **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs) + 1,
+ f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
+ *args, **kwargs
+ )
+ + 1,
)
```