mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-21 19:05:09 +00:00
Special ExprTuple
formatting option for for
-loops (#5175)
## Motivation While black keeps parentheses nearly everywhere, the notable exception is in the body of for loops: ```python for (a, b) in x: pass ``` becomes ```python for a, b in x: pass ``` This currently blocks #5163, which this PR should unblock. ## Solution This changes the `ExprTuple` formatting option to include one additional option that removes the parentheses when not using magic trailing comma and not breaking. It is supposed to be used through ```rust #[derive(Debug)] struct ExprTupleWithoutParentheses<'a>(&'a Expr); impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> { fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> { match self.0 { Expr::Tuple(expr_tuple) => expr_tuple .format() .with_options(TupleParentheses::StripInsideForLoop) .fmt(f), other => other.format().with_options(Parenthesize::IfBreaks).fmt(f), } } } ``` ## Testing The for loop formatting isn't merged due to missing this (and i didn't want to create more git weirdness across two people), but I've confirmed that when applying this to while loops instead of for loops, then ```rust write!( f, [ text("while"), space(), ExprTupleWithoutParentheses(test.as_ref()), text(":"), trailing_comments(trailing_condition_comments), block_indent(&body.format()) ] )?; ``` makes ```python while (a, b): pass while ( ajssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssa, b, ): pass while (a,b,): pass ``` formatted as ```python while a, b: pass while ( ajssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssa, b, ): pass while ( a, b, ): pass ```
This commit is contained in:
parent
9b5fb8f38f
commit
9419d3f9c8
2 changed files with 50 additions and 6 deletions
|
@ -1,5 +1,6 @@
|
|||
use crate::comments::Comments;
|
||||
use crate::context::NodeLevel;
|
||||
use crate::expression::expr_tuple::TupleParentheses;
|
||||
use crate::expression::parentheses::{NeedsParentheses, Parentheses, Parenthesize};
|
||||
use crate::prelude::*;
|
||||
use ruff_formatter::{
|
||||
|
@ -85,7 +86,10 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
|
|||
Expr::Starred(expr) => expr.format().fmt(f),
|
||||
Expr::Name(expr) => expr.format().fmt(f),
|
||||
Expr::List(expr) => expr.format().fmt(f),
|
||||
Expr::Tuple(expr) => expr.format().fmt(f),
|
||||
Expr::Tuple(expr) => expr
|
||||
.format()
|
||||
.with_options(TupleParentheses::Expr(parentheses))
|
||||
.fmt(f),
|
||||
Expr::Slice(expr) => expr.format().fmt(f),
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue