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:
Brent Westbrook 2025-12-12 12:57:09 -05:00 committed by GitHub
parent 69d1bfbebc
commit dec4154c8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 96 additions and 1 deletions

View File

@ -722,7 +722,7 @@ with tempfile.TemporaryDirectory() as d1:
### 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:
```python
@ -742,3 +742,98 @@ items = [(True)]
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
),
)
```