mirror of https://github.com/astral-sh/ruff
Document known lambda formatting deviations from Black (#21954)
Summary -- Following #8179, we now format long lambda expressions a bit more like Black, preferring to keep long parameter lists on a single line, but we go one step further to break the body itself across multiple lines and parenthesize it if it's still too long. This PR documents both the stable deviation that breaks parameters across multiple lines, and the new preview deviation that breaks the body instead. I also fixed a couple of typos in the section immediately above my addition. Test Plan -- I tested all of the snippets here against `main` for the preview behavior, our playground for the stable behavior, and Black's playground for their behavior
This commit is contained in:
parent
69d1bfbebc
commit
dec4154c8a
|
|
@ -722,7 +722,7 @@ with tempfile.TemporaryDirectory() as d1:
|
||||||
|
|
||||||
### Preserving parentheses around single-element lists
|
### Preserving parentheses around single-element lists
|
||||||
|
|
||||||
Ruff preserves at least one parentheses around list elements, even if the list only contains a single element. The Black 2025 or newer, on the other hand, removes the parentheses
|
Ruff preserves at least one set of parentheses around list elements, even if the list only contains a single element. The Black 2025 style or newer, on the other hand, removes the parentheses
|
||||||
for single-element lists if they aren't multiline and doing so does not change semantics:
|
for single-element lists if they aren't multiline and doing so does not change semantics:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
|
@ -742,3 +742,98 @@ items = [(True)]
|
||||||
items = {(123)}
|
items = {(123)}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Long lambda expressions
|
||||||
|
|
||||||
|
In [preview](../preview.md), Ruff will keep lambda parameters on a single line,
|
||||||
|
just like Black:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Input
|
||||||
|
def a():
|
||||||
|
return b(
|
||||||
|
c,
|
||||||
|
d,
|
||||||
|
e,
|
||||||
|
f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||||
|
*args, **kwargs
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Ruff Stable
|
||||||
|
def a():
|
||||||
|
return b(
|
||||||
|
c,
|
||||||
|
d,
|
||||||
|
e,
|
||||||
|
f=lambda self,
|
||||||
|
*args,
|
||||||
|
**kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Black and Ruff Preview
|
||||||
|
def a():
|
||||||
|
return b(
|
||||||
|
c,
|
||||||
|
d,
|
||||||
|
e,
|
||||||
|
f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||||
|
*args, **kwargs
|
||||||
|
),
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
However, if the body expression exceeds the configured line length, Ruff will
|
||||||
|
additionally add parentheses around the lambda body and break it over multiple
|
||||||
|
lines:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Input
|
||||||
|
def a():
|
||||||
|
return b(
|
||||||
|
c,
|
||||||
|
d,
|
||||||
|
e,
|
||||||
|
# Additional `b` character pushes this over the line length
|
||||||
|
f=lambda self, *args, **kwargs: baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||||
|
*args, **kwargs
|
||||||
|
),
|
||||||
|
# More complex expressions also trigger wrapping
|
||||||
|
g=lambda self, *args, **kwargs: baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||||
|
*args, **kwargs
|
||||||
|
) + 1,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Black
|
||||||
|
def a():
|
||||||
|
return b(
|
||||||
|
c,
|
||||||
|
d,
|
||||||
|
e,
|
||||||
|
# Additional `b` character pushes this over the line length
|
||||||
|
f=lambda self, *args, **kwargs: baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||||
|
*args, **kwargs
|
||||||
|
),
|
||||||
|
# More complex expressions also trigger wrapping
|
||||||
|
g=lambda self, *args, **kwargs: baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||||
|
*args, **kwargs
|
||||||
|
)
|
||||||
|
+ 1,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Ruff Preview
|
||||||
|
def a():
|
||||||
|
return b(
|
||||||
|
c,
|
||||||
|
d,
|
||||||
|
e,
|
||||||
|
# Additional `b` character pushes this over the line length
|
||||||
|
f=lambda self, *args, **kwargs: (
|
||||||
|
baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs)
|
||||||
|
),
|
||||||
|
# More complex expressions also trigger wrapping
|
||||||
|
g=lambda self, *args, **kwargs: (
|
||||||
|
baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs) + 1
|
||||||
|
),
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue