Skip to content

Commit dca430f

Browse files
authored
Fix instability with await fluent style (#8676)
Fix an instability where await was followed by a breaking fluent style expression: ```python test_data = await ( Stream.from_async(async_data) .flat_map_async() .map() .filter_async(is_valid_data) .to_list() ) ``` Note that this technically a minor style change (see ecosystem check)
1 parent 841e6c8 commit dca430f

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/await.py

+9
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,12 @@
4848
# comment
4949
[foo]
5050
)
51+
52+
# https://github.com/astral-sh/ruff/issues/8644
53+
test_data = await (
54+
Stream.from_async(async_data)
55+
.flat_map_async()
56+
.map()
57+
.filter_async(is_valid_data)
58+
.to_list()
59+
)

crates/ruff_python_formatter/src/expression/expr_await.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl FormatNodeRule<ExprAwait> for FormatExprAwait {
2020
[
2121
token("await"),
2222
space(),
23-
maybe_parenthesize_expression(value, item, Parenthesize::IfRequired)
23+
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
2424
]
2525
)
2626
}

crates/ruff_python_formatter/src/expression/parentheses.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::comments::{
1414
use crate::context::{NodeLevel, WithNodeLevel};
1515
use crate::prelude::*;
1616

17+
/// From the perspective of the expression, under which circumstances does it need parentheses
1718
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1819
pub(crate) enum OptionalParentheses {
1920
/// Add parentheses if the expression expands over multiple lines
@@ -41,7 +42,8 @@ pub(crate) trait NeedsParentheses {
4142
) -> OptionalParentheses;
4243
}
4344

44-
/// Configures if the expression should be parenthesized.
45+
/// From the perspective of the parent statement or expression, when should the child expression
46+
/// get parentheses?
4547
#[derive(Copy, Clone, Debug, PartialEq)]
4648
pub(crate) enum Parenthesize {
4749
/// Parenthesizes the expression if it doesn't fit on a line OR if the expression is parenthesized in the source code.

crates/ruff_python_formatter/tests/snapshots/black_compatibility@simple_cases__remove_await_parens.py.snap

+11-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async def main():
9393
```diff
9494
--- Black
9595
+++ Ruff
96-
@@ -21,7 +21,9 @@
96+
@@ -21,11 +21,15 @@
9797
9898
# Check comments
9999
async def main():
@@ -103,6 +103,13 @@ async def main():
103103
+ )
104104
105105
106+
async def main():
107+
- await asyncio.sleep(1) # Hello
108+
+ await (
109+
+ asyncio.sleep(1) # Hello
110+
+ )
111+
112+
106113
async def main():
107114
```
108115

@@ -138,7 +145,9 @@ async def main():
138145
139146
140147
async def main():
141-
await asyncio.sleep(1) # Hello
148+
await (
149+
asyncio.sleep(1) # Hello
150+
)
142151
143152
144153
async def main():

crates/ruff_python_formatter/tests/snapshots/format@expression__await.py.snap

+18
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ await (
5454
# comment
5555
[foo]
5656
)
57+
58+
# https://github.com/astral-sh/ruff/issues/8644
59+
test_data = await (
60+
Stream.from_async(async_data)
61+
.flat_map_async()
62+
.map()
63+
.filter_async(is_valid_data)
64+
.to_list()
65+
)
5766
```
5867

5968
## Output
@@ -122,6 +131,15 @@ await (
122131
# comment
123132
[foo]
124133
)
134+
135+
# https://github.com/astral-sh/ruff/issues/8644
136+
test_data = await (
137+
Stream.from_async(async_data)
138+
.flat_map_async()
139+
.map()
140+
.filter_async(is_valid_data)
141+
.to_list()
142+
)
125143
```
126144

127145

0 commit comments

Comments
 (0)