Skip to content

Commit

Permalink
Fix instability with await fluent style (#8676)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
konstin authored Nov 17, 2023
1 parent 841e6c8 commit dca430f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@
# comment
[foo]
)

# https://github.com/astral-sh/ruff/issues/8644
test_data = await (
Stream.from_async(async_data)
.flat_map_async()
.map()
.filter_async(is_valid_data)
.to_list()
)
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/expression/expr_await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl FormatNodeRule<ExprAwait> for FormatExprAwait {
[
token("await"),
space(),
maybe_parenthesize_expression(value, item, Parenthesize::IfRequired)
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
]
)
}
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_python_formatter/src/expression/parentheses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::comments::{
use crate::context::{NodeLevel, WithNodeLevel};
use crate::prelude::*;

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

/// Configures if the expression should be parenthesized.
/// From the perspective of the parent statement or expression, when should the child expression
/// get parentheses?
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) enum Parenthesize {
/// Parenthesizes the expression if it doesn't fit on a line OR if the expression is parenthesized in the source code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def main():
```diff
--- Black
+++ Ruff
@@ -21,7 +21,9 @@
@@ -21,11 +21,15 @@
# Check comments
async def main():
Expand All @@ -103,6 +103,13 @@ async def main():
+ )
async def main():
- await asyncio.sleep(1) # Hello
+ await (
+ asyncio.sleep(1) # Hello
+ )
async def main():
```

Expand Down Expand Up @@ -138,7 +145,9 @@ async def main():
async def main():
await asyncio.sleep(1) # Hello
await (
asyncio.sleep(1) # Hello
)
async def main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ await (
# comment
[foo]
)
# https://github.com/astral-sh/ruff/issues/8644
test_data = await (
Stream.from_async(async_data)
.flat_map_async()
.map()
.filter_async(is_valid_data)
.to_list()
)
```

## Output
Expand Down Expand Up @@ -122,6 +131,15 @@ await (
# comment
[foo]
)
# https://github.com/astral-sh/ruff/issues/8644
test_data = await (
Stream.from_async(async_data)
.flat_map_async()
.map()
.filter_async(is_valid_data)
.to_list()
)
```


Expand Down

0 comments on commit dca430f

Please sign in to comment.