mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:26:23 +00:00
Fix multiline lambda expression statement formating (#8466)
## Summary This PR fixes a bug in our formatter where a multiline lambda expression statement was formatted over multiple lines without adding parentheses. The PR "fixes" the problem by not splitting the lambda parameters if it is not parenthesized ## Test Plan Added test
This commit is contained in:
parent
75c9be099f
commit
e57bccd500
3 changed files with 95 additions and 3 deletions
|
@ -203,3 +203,28 @@ lambda: ( # comment
|
|||
y:
|
||||
z
|
||||
)
|
||||
|
||||
lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
|
||||
def a():
|
||||
return b(
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||
*args, **kwargs
|
||||
),
|
||||
)
|
||||
|
||||
def a():
|
||||
return b(
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
f=lambda self, araa, kkkwargs,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
args,kwargs,
|
||||
e=1, f=2, g=2: d,
|
||||
g = 10
|
||||
)
|
||||
|
||||
|
|
|
@ -102,7 +102,15 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
dangling.split_at(parenthesis_comments_end);
|
||||
|
||||
let format_inner = format_with(|f: &mut PyFormatter| {
|
||||
let separator = format_with(|f| write!(f, [token(","), soft_line_break_or_space()]));
|
||||
let separator = format_with(|f: &mut PyFormatter| {
|
||||
token(",").fmt(f)?;
|
||||
|
||||
if f.context().node_level().is_parenthesized() {
|
||||
soft_line_break_or_space().fmt(f)
|
||||
} else {
|
||||
space().fmt(f)
|
||||
}
|
||||
});
|
||||
let mut joiner = f.join_with(separator);
|
||||
let mut last_node: Option<AnyNodeRef> = None;
|
||||
|
||||
|
@ -232,8 +240,6 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
Ok(())
|
||||
});
|
||||
|
||||
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
|
||||
|
||||
let num_parameters = posonlyargs.len()
|
||||
+ args.len()
|
||||
+ usize::from(vararg.is_some())
|
||||
|
@ -243,12 +249,14 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
if self.parentheses == ParametersParentheses::Never {
|
||||
write!(f, [group(&format_inner), dangling_comments(dangling)])
|
||||
} else if num_parameters == 0 {
|
||||
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
|
||||
// No parameters, format any dangling comments between `()`
|
||||
write!(f, [empty_parenthesized("(", dangling, ")")])
|
||||
} else {
|
||||
// Intentionally avoid `parenthesized`, which groups the entire formatted contents.
|
||||
// We want parameters to be grouped alongside return types, one level up, so we
|
||||
// format them "inline" here.
|
||||
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
|
|
|
@ -209,6 +209,31 @@ lambda: ( # comment
|
|||
y:
|
||||
z
|
||||
)
|
||||
|
||||
lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
|
||||
def a():
|
||||
return b(
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||
*args, **kwargs
|
||||
),
|
||||
)
|
||||
|
||||
def a():
|
||||
return b(
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
f=lambda self, araa, kkkwargs,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
args,kwargs,
|
||||
e=1, f=2, g=2: d,
|
||||
g = 10
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
## Output
|
||||
|
@ -413,6 +438,40 @@ lambda: ( # comment
|
|||
# comment
|
||||
y: z
|
||||
)
|
||||
|
||||
lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||
*args, **kwargs
|
||||
), e=1, f=2, g=2: d
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
|
||||
def a():
|
||||
return b(
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
f=lambda self,
|
||||
*args,
|
||||
**kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs),
|
||||
)
|
||||
|
||||
|
||||
def a():
|
||||
return b(
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
f=lambda self,
|
||||
araa,
|
||||
kkkwargs,
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
args,
|
||||
kwargs,
|
||||
e=1,
|
||||
f=2,
|
||||
g=2: d,
|
||||
g=10,
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue