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
10 changes: 1 addition & 9 deletions crates/ruff_python_formatter/src/expression/expr_lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::expression::parentheses::{
use crate::expression::{CallChainLayout, has_own_parentheses};
use crate::other::parameters::ParametersParentheses;
use crate::prelude::*;
use crate::preview::is_parenthesize_lambda_bodies_enabled;

#[derive(Default)]
pub struct FormatExprLambda {
Expand All @@ -31,7 +30,6 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {

let comments = f.context().comments().clone();
let dangling = comments.dangling(item);
let preview = is_parenthesize_lambda_bodies_enabled(f.context());

write!(f, [token("lambda")])?;

Expand Down Expand Up @@ -108,7 +106,7 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
}

// Try to keep the parameters on a single line, unless there are intervening comments.
if preview && !comments.contains_comments(parameters.into()) {
if !comments.contains_comments(parameters.into()) {
let mut buffer = RemoveSoftLinesBuffer::new(f);
write!(
buffer,
Expand All @@ -134,12 +132,6 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {

if dangling_header_comments.is_empty() {
write!(f, [space()])?;
} else if !preview {
write!(f, [dangling_comments(dangling_header_comments)])?;
}

if !preview {
return body.format().fmt(f);
}

let fmt_body = FormatBody {
Expand Down
7 changes: 0 additions & 7 deletions crates/ruff_python_formatter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,3 @@ pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
) -> bool {
context.is_preview()
}

/// Returns `true` if the
/// [`parenthesize_lambda_bodies`](https://github.com/astral-sh/ruff/pull/21385) preview style is
/// enabled.
pub(crate) const fn is_parenthesize_lambda_bodies_enabled(context: &PyFormatContext) -> bool {
context.is_preview()
}
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/statement/stmt_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::expression::{
};
use crate::other::interpolated_string::InterpolatedStringLayout;
use crate::prelude::*;
use crate::preview::is_parenthesize_lambda_bodies_enabled;
use crate::statement::trailing_semicolon;
use crate::string::StringLikeExtensions;
use crate::string::implicit::{
Expand Down Expand Up @@ -1355,7 +1354,7 @@ fn is_attribute_with_parenthesized_value(target: &Expr, context: &PyFormatContex
}
}

/// Like [`maybe_parenthesize_expression`] but with special handling for lambdas in preview.
/// Like [`maybe_parenthesize_expression`] but with special handling for lambdas.
fn maybe_parenthesize_value<'a>(
expression: &'a Expr,
parent: AnyNodeRef<'a>,
Expand All @@ -1372,8 +1371,7 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeValue<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let MaybeParenthesizeValue { expression, parent } = self;

if is_parenthesize_lambda_bodies_enabled(f.context())
&& let Expr::Lambda(lambda) = expression
if let Expr::Lambda(lambda) = expression
&& !f.context().comments().has_leading(lambda)
{
parenthesize_if_expands(&lambda.format().with_options(ExprLambdaLayout::Assignment))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/cases/comments_in_blocks.py
---
## Input

Expand Down Expand Up @@ -123,29 +122,43 @@ if (
```diff
--- Black
+++ Ruff
@@ -5,9 +5,9 @@
@@ -5,23 +5,25 @@
# - https://github.com/psf/black/issues/563

(
- lambda
+ lambda:
# a comment
- # a comment
- : None
+ None
+ lambda: (
+ # a comment
+ None
+ )
)

(
@@ -17,9 +17,8 @@
- lambda:
- # b comment
- None
+ lambda: (
+ # b comment
+ None
+ )
)

(
- lambda
+ lambda:
# a comment
- # a comment
- :
# b comment
None
- # b comment
- None
+ lambda: (
+ # a comment
+ # b comment
+ None
+ )
)

[
```

## Ruff Output
Expand All @@ -158,22 +171,25 @@ if (
# - https://github.com/psf/black/issues/563

(
lambda:
# a comment
None
lambda: (
# a comment
None
)
)

(
lambda:
# b comment
None
lambda: (
# b comment
None
)
)

(
lambda:
# a comment
# b comment
None
lambda: (
# a comment
# b comment
None
)
)

[
Expand Down
Loading