Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,25 @@ def a():
x:
x
)

(
lambda
# comment
*x,
**y: x
)

(
lambda
* # comment 2
x,
**y:
x
)

(
lambda
** # comment 1
x:
x
)
21 changes: 16 additions & 5 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,20 @@ fn handle_parameter_comment<'a>(
CommentPlacement::Default(comment)
}
} else if comment.start() < parameter.name.start() {
CommentPlacement::leading(parameter, comment)
// For lambdas, where the parameters cannot be parenthesized and the first parameter thus
// starts at the same position as the parent parameters, mark a comment before the first
// parameter as leading on the parameters rather than the individual parameter to prevent
// the whole parameter list from breaking.
//
// Note that this check is not needed above because lambda parameters cannot have
// annotations.
if let Some(AnyNodeRef::Parameters(parameters)) = comment.enclosing_parent()
&& parameters.start() == parameter.start()
{
CommentPlacement::leading(parameters, comment)
} else {
CommentPlacement::leading(parameter, comment)
}
} else {
CommentPlacement::Default(comment)
}
Expand Down Expand Up @@ -1835,10 +1848,8 @@ fn handle_lambda_comment<'a>(
// )
// ```
if comment.start() < parameters.start() {
return if let Some(first) = parameters.iter().next()
&& comment.line_position().is_own_line()
{
CommentPlacement::leading(first.as_parameter(), comment)
return if comment.line_position().is_own_line() {
CommentPlacement::leading(parameters, comment)
} else {
CommentPlacement::dangling(comment.enclosing_node(), comment)
};
Expand Down
10 changes: 3 additions & 7 deletions crates/ruff_python_formatter/src/expression/expr_lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
.split_at(dangling.partition_point(|comment| comment.end() < parameters.start()));

if dangling_before_parameters.is_empty() {
// If the first parameter has a leading comment, insert a hard line break. This
// comment is associated as a leading comment on the first parameter:
// If the parameters have a leading comment, insert a hard line break. This
// comment is associated as a leading comment on the parameters:
//
// ```py
// (
Expand Down Expand Up @@ -86,11 +86,7 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
// *x: x
// )
// ```
if parameters
.iter()
.next()
.is_some_and(|parameter| comments.has_leading(parameter.as_parameter()))
{
if comments.has_leading(&**parameters) {
hard_line_break().fmt(f)?;
} else {
write!(f, [space()])?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,28 @@ def a():
x:
x
)

(
lambda
# comment
*x,
**y: x
)

(
lambda
* # comment 2
x,
**y:
x
)

(
lambda
** # comment 1
x:
x
)
```

## Output
Expand Down Expand Up @@ -513,4 +535,22 @@ def a():
# comment 2
*x: x
)

(
lambda
# comment
*x, **y: x
)

(
lambda
# comment 2
*x, **y: x
)

(
lambda
# comment 1
**x: x
)
```