Fix instability with await fluent style (#8676) · astral-sh/ruff@dca430f (original) (raw)

File tree

5 files changed

lines changed

5 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -48,3 +48,12 @@
48 48 # comment
49 49 [foo]
50 50 )
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 +)
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ impl FormatNodeRule for FormatExprAwait {
20 20 [
21 21 token("await"),
22 22 space(),
23 - maybe_parenthesize_expression(value, item, Parenthesize::IfRequired)
23 + maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
24 24 ]
25 25 )
26 26 }
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ use crate::comments::{
14 14 use crate::context::{NodeLevel, WithNodeLevel};
15 15 use crate::prelude::*;
16 16
17 +/// From the perspective of the expression, under which circumstances does it need parentheses
17 18 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
18 19 pub(crate) enum OptionalParentheses {
19 20 /// Add parentheses if the expression expands over multiple lines
@@ -41,7 +42,8 @@ pub(crate) trait NeedsParentheses {
41 42 ) -> OptionalParentheses;
42 43 }
43 44
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?
45 47 #[derive(Copy, Clone, Debug, PartialEq)]
46 48 pub(crate) enum Parenthesize {
47 49 /// Parenthesizes the expression if it doesn't fit on a line OR if the expression is parenthesized in the source code.
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ async def main():
93 93 ```diff
94 94 --- Black
95 95 +++ Ruff
96 -@@ -21,7 +21,9 @@
96 +@@ -21,11 +21,15 @@
97 97
98 98 # Check comments
99 99 async def main():
@@ -103,6 +103,13 @@ async def main():
103 103 + )
104 104
105 105
106 + async def main():
107 +- await asyncio.sleep(1) # Hello
108 ++ await (
109 ++ asyncio.sleep(1) # Hello
110 ++ )
111 +
112 +
106 113 async def main():
107 114 ```
108 115
@@ -138,7 +145,9 @@ async def main():
138 145
139 146
140 147 async def main():
141 - await asyncio.sleep(1) # Hello
148 + await (
149 + asyncio.sleep(1) # Hello
150 + )
142 151
143 152
144 153 async def main():
Original file line number Diff line number Diff line change
@@ -54,6 +54,15 @@ await (
54 54 # comment
55 55 [foo]
56 56 )
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 +)
57 66 ```
58 67
59 68 ## Output
@@ -122,6 +131,15 @@ await (
122 131 # comment
123 132 [foo]
124 133 )
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 +)
125 143 ```
126 144
127 145