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)
This commit is contained in:
konsti 2023-11-17 18:24:19 +01:00 committed by GitHub
parent 841e6c889e
commit dca430f4d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 4 deletions

View file

@ -48,3 +48,12 @@ await (
# comment # comment
[foo] [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()
)

View file

@ -20,7 +20,7 @@ impl FormatNodeRule<ExprAwait> for FormatExprAwait {
[ [
token("await"), token("await"),
space(), space(),
maybe_parenthesize_expression(value, item, Parenthesize::IfRequired) maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
] ]
) )
} }

View file

@ -14,6 +14,7 @@ use crate::comments::{
use crate::context::{NodeLevel, WithNodeLevel}; use crate::context::{NodeLevel, WithNodeLevel};
use crate::prelude::*; use crate::prelude::*;
/// From the perspective of the expression, under which circumstances does it need parentheses
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum OptionalParentheses { pub(crate) enum OptionalParentheses {
/// Add parentheses if the expression expands over multiple lines /// Add parentheses if the expression expands over multiple lines
@ -41,7 +42,8 @@ pub(crate) trait NeedsParentheses {
) -> OptionalParentheses; ) -> 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)] #[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) enum Parenthesize { 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. /// Parenthesizes the expression if it doesn't fit on a line OR if the expression is parenthesized in the source code.

View file

@ -93,7 +93,7 @@ async def main():
```diff ```diff
--- Black --- Black
+++ Ruff +++ Ruff
@@ -21,7 +21,9 @@ @@ -21,11 +21,15 @@
# Check comments # Check comments
async def main(): async def main():
@ -103,6 +103,13 @@ async def main():
+ ) + )
async def main():
- await asyncio.sleep(1) # Hello
+ await (
+ asyncio.sleep(1) # Hello
+ )
async def main(): async def main():
``` ```
@ -138,7 +145,9 @@ async def main():
async def main(): async def main():
await asyncio.sleep(1) # Hello await (
asyncio.sleep(1) # Hello
)
async def main(): async def main():

View file

@ -54,6 +54,15 @@ await (
# comment # comment
[foo] [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 ## Output
@ -122,6 +131,15 @@ await (
# comment # comment
[foo] [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()
)
``` ```