Skip to content

Commit 6e263eb

Browse files
committed
Use a new parenthesized mode
1 parent c655ef0 commit 6e263eb

File tree

5 files changed

+37
-402
lines changed

5 files changed

+37
-402
lines changed

crates/ruff_python_formatter/src/expression/expr_named_expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ impl NeedsParentheses for ExprNamedExpr {
4646
|| parent.is_with_item()
4747
|| parent.is_stmt_delete()
4848
|| parent.is_stmt_for()
49-
|| parent.is_stmt_function_def()
5049
{
5150
OptionalParentheses::Always
5251
} else {

crates/ruff_python_formatter/src/expression/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
183183
needs_parentheses
184184
}
185185
}
186-
Parenthesize::Optional | Parenthesize::IfBreaks => needs_parentheses,
186+
Parenthesize::Optional
187+
| Parenthesize::IfBreaks
188+
| Parenthesize::IfBreaksOrIfRequired => needs_parentheses,
187189
};
188190

189191
match needs_parentheses {
@@ -199,6 +201,10 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
199201
OptionalParentheses::Always => {
200202
expression.format().with_options(Parentheses::Always).fmt(f)
201203
}
204+
OptionalParentheses::Never if *parenthesize == Parenthesize::IfBreaksOrIfRequired => {
205+
parenthesize_if_expands(&expression.format().with_options(Parentheses::Never))
206+
.fmt(f)
207+
}
202208
OptionalParentheses::Never | OptionalParentheses::Multiline => {
203209
expression.format().with_options(Parentheses::Never).fmt(f)
204210
}

crates/ruff_python_formatter/src/expression/parentheses.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ pub(crate) enum Parenthesize {
5151
/// * The expression is not enclosed by another parenthesized expression and it expands over multiple lines
5252
/// * The expression has leading or trailing comments. Adding parentheses is desired to prevent the comments from wandering.
5353
IfRequired,
54+
55+
/// Parenthesizes the expression if the group doesn't fit on a line (e.g., even name expressions are parenthesized), or if
56+
/// the expression doesn't break, but _does_ reports that it always requires parentheses in this position (e.g., walrus
57+
/// operators in function return annotations).
58+
IfBreaksOrIfRequired,
5459
}
5560

5661
impl Parenthesize {

crates/ruff_python_formatter/src/statement/stmt_function_def.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use ruff_formatter::write;
2-
32
use ruff_python_ast::{Parameters, Ranged, StmtFunctionDef};
43
use ruff_python_trivia::{lines_after_ignoring_trivia, SimpleTokenKind, SimpleTokenizer};
54

65
use crate::comments::{leading_comments, trailing_comments};
76
use crate::expression::maybe_parenthesize_expression;
8-
use crate::expression::parentheses::{optional_parentheses, Parentheses, Parenthesize};
7+
use crate::expression::parentheses::Parenthesize;
98
use crate::prelude::*;
109
use crate::statement::suite::SuiteKind;
1110
use crate::FormatNodeRule;
@@ -65,37 +64,31 @@ impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
6564

6665
if let Some(return_annotation) = item.returns.as_ref() {
6766
write!(f, [space(), text("->"), space()])?;
68-
69-
if empty_parameters(&item.parameters, f.context().source()) {
70-
// If the parameters are empty, add parentheses if the return annotation
71-
// breaks at all.
72-
write!(
73-
f,
74-
[optional_parentheses(
75-
&return_annotation.format().with_options(Parentheses::Never),
76-
)]
77-
)?;
78-
} else {
79-
// Otherwise, use our normal rules for parentheses, which allows us to break
80-
// like:
81-
// ```python
82-
// def f(
83-
// x,
84-
// ) -> Tuple[
85-
// int,
86-
// int,
87-
// ]:
88-
// ...
89-
// ```
90-
write!(
91-
f,
92-
[maybe_parenthesize_expression(
93-
return_annotation,
94-
item,
67+
write!(
68+
f,
69+
[maybe_parenthesize_expression(
70+
return_annotation,
71+
item,
72+
if empty_parameters(&item.parameters, f.context().source()) {
73+
// If the parameters are empty, add parentheses if the return annotation
74+
// breaks at all.
75+
Parenthesize::IfBreaksOrIfRequired
76+
} else {
77+
// Otherwise, use our normal rules for parentheses, which allows us to break
78+
// like:
79+
// ```python
80+
// def f(
81+
// x,
82+
// ) -> Tuple[
83+
// int,
84+
// int,
85+
// ]:
86+
// ...
87+
// ```
9588
Parenthesize::IfBreaks
96-
)]
97-
)?;
98-
};
89+
}
90+
)]
91+
)?;
9992
}
10093
Ok(())
10194
});

0 commit comments

Comments
 (0)