You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -93,22 +107,36 @@ Do notation for Result (syntactic sugar for sequence of `and_then()` calls).
93
107
94
108
95
109
96
-
Usage: ``` # This is similar to```
110
+
Usage:
111
+
112
+
```rust
113
+
114
+
// This is similar to
97
115
usedo_notation::m;
98
116
letfinal_result=m! {
99
117
x<-Ok("hello");
100
118
y<-Ok(True);
101
119
Ok(len(x) +int(y) +0.5)
102
120
};
103
121
104
-
``` final_result: Result[float, int] = do(``` Ok(len(x) + int(y) + 0.5) for x in Ok("hello") for y in Ok(True) )
122
+
```
123
+
124
+
```rust
125
+
126
+
final_result:Result[float, int] =do(
127
+
Ok(len(x) +int(y) +0.5)
128
+
forxinOk("hello")
129
+
foryinOk(True)
130
+
)
131
+
132
+
```
105
133
106
134
NOTE: If you exclude the type annotation e.g. `Result[float, int]` your type checker might be unable to infer the return type. To avoid an error, you might need to help it with the type hint.
final_result: Result[float, int] =await do_async( Ok(len(x) +int(y) + z) for x inawait get_async_result_1() for y inawait get_async_result_2() for z in get_sync_result_3() )
153
+
154
+
```
129
155
130
156
NOTE: Python makes generators async in a counter-intuitive way.
131
-
This is a regular generator:
132
-
async def foo(): ...
133
-
do(Ok(1) for x in await foo())
134
-
135
-
But this is an async generator:
136
-
async def foo(): ...
137
-
async def bar(): ...
138
-
do(
139
-
Ok(1)
140
-
for x in await foo()
141
-
for y in await bar()
142
-
)
157
+
158
+
```python
159
+
160
+
# This is a regular generator: async def foo(): ... do(Ok(1) for x in await foo())
161
+
162
+
```
163
+
164
+
```python
165
+
166
+
# But this is an async generator: async def foo(): ... async def bar(): ... do( Ok(1) for x in await foo() for y in await bar() )
167
+
168
+
```
143
169
144
170
We let users try to use regular `do()`, which works in some cases
145
171
of awaiting async values. If we hit a case like above, we raise
@@ -149,14 +175,18 @@ See `do()`.
149
175
However, for better usability, it's better for `do_async()` to also accept
150
176
regular generators, as you get in the first case:
151
177
152
-
async def foo(): ...
153
-
do(Ok(1) for x in await foo())
178
+
```python
179
+
180
+
asyncdeffoo(): ... do(Ok(1) for x inawait foo())
181
+
182
+
```
154
183
155
184
Furthermore, neither mypy nor pyright can infer that the second case is
156
185
actually an async generator, so we cannot annotate `do_async()`
157
186
as accepting only an async generator. This is additional motivation
0 commit comments