From 90f43bde8497059b231ea11c66e0dd2e80715fb6 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Tue, 9 Dec 2025 14:35:39 -0500 Subject: [PATCH 1/2] add broken test cases the new leading comment is causing the whole Parameters list to break. these cases should instead format like: ```py ( lambda # comment *x, **y: x ) ( lambda # comment 2 *x, **y: x ) ``` without line breaks in the parameter list --- .../test/fixtures/ruff/expression/lambda.py | 22 ++++++++++ .../format@expression__lambda.py.snap | 42 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/lambda.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/lambda.py index 660d5644e9..1b1c1ee3c2 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/lambda.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/lambda.py @@ -249,3 +249,25 @@ def a(): x: x ) + +( + lambda + # comment + *x, + **y: x +) + +( + lambda + * # comment 2 + x, + **y: + x +) + +( + lambda + ** # comment 1 + x: + x +) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap index 3009dfaefc..c489d18933 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap @@ -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 @@ -513,4 +535,24 @@ def a(): # comment 2 *x: x ) + +( + lambda + # comment + *x, + **y: x +) + +( + lambda + # comment 2 + *x, + **y: x +) + +( + lambda + # comment 1 + **x: x +) ``` From b0a82983af6c46b5a4ce2794624697479be756b2 Mon Sep 17 00:00:00 2001 From: Brent Westbrook Date: Tue, 9 Dec 2025 15:32:24 -0500 Subject: [PATCH 2/2] avoid breaking when the first parameter has leading comments --- .../src/other/parameters.rs | 26 +++++++++++++++++++ .../format@expression__lambda.py.snap | 6 ++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/crates/ruff_python_formatter/src/other/parameters.rs b/crates/ruff_python_formatter/src/other/parameters.rs index 1c6682bab1..2e12f48456 100644 --- a/crates/ruff_python_formatter/src/other/parameters.rs +++ b/crates/ruff_python_formatter/src/other/parameters.rs @@ -241,6 +241,32 @@ impl FormatNodeRule for FormatParameters { let num_parameters = item.len(); if self.parentheses == ParametersParentheses::Never { + // In a lambda, format any leading comments on the first parameter outside of the + // parameters group so that the parameters don't break. For example, format this input: + // + // ```py + // ( + // lambda + // * # this becomes a leading comment on *x + // x, **y: + // x + // ) + // ``` + // + // as: + // + // ```py + // ( + // lambda + // # this becomes a leading comment on *x + // *x, **y: x + // ) + // ``` + if let Some(first) = item.iter().next() + && comments.has_leading(first.as_parameter()) + { + leading_node_comments(first.as_parameter()).fmt(f)?; + } write!(f, [group(&format_inner), dangling_comments(dangling)]) } else if num_parameters == 0 { let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f); diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap index c489d18933..5997ff539a 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__lambda.py.snap @@ -539,15 +539,13 @@ def a(): ( lambda # comment - *x, - **y: x + *x, **y: x ) ( lambda # comment 2 - *x, - **y: x + *x, **y: x ) (