MCPcopy Index your code
hub / github.com/dry-python/returns / do

Method do

returns/future.py:1179–1220  ·  view source on GitHub ↗

Allows working with unwrapped values of containers in a safe way. .. code:: python >>> import anyio >>> from returns.future import FutureResult >>> from returns.io import IOSuccess, IOFailure >>> async def success() -> bool: ...

(
        cls,
        expr: AsyncGenerator[_NewValueType, None],
    )

Source from the content-addressed store, hash-verified

1177
1178 @classmethod
1179 def do(
1180 cls,
1181 expr: AsyncGenerator[_NewValueType, None],
1182 ) -> 'FutureResult[_NewValueType, _NewErrorType]':
1183 """
1184 Allows working with unwrapped values of containers in a safe way.
1185
1186 .. code:: python
1187
1188 >>> import anyio
1189 >>> from returns.future import FutureResult
1190 >>> from returns.io import IOSuccess, IOFailure
1191
1192 >>> async def success() -> bool:
1193 ... return await FutureResult.do(
1194 ... first + second
1195 ... async for first in FutureResult.from_value(2)
1196 ... async for second in FutureResult.from_value(3)
1197 ... ) == IOSuccess(5)
1198
1199 >>> assert anyio.run(success) is True
1200
1201 >>> async def failure() -> bool:
1202 ... return await FutureResult.do(
1203 ... first + second
1204 ... async for first in FutureResult.from_value(2)
1205 ... async for second in FutureResult.from_failure(3)
1206 ... ) == IOFailure(3)
1207
1208 >>> assert anyio.run(failure) is True
1209
1210 See :ref:`do-notation` to learn more.
1211
1212 """
1213
1214 async def factory() -> Result[_NewValueType, _NewErrorType]:
1215 try:
1216 return Success(await anext(expr))
1217 except UnwrapFailedError as exc:
1218 return exc.halted_container # type: ignore
1219
1220 return FutureResult(factory())
1221
1222 @classmethod
1223 def from_typecast(

Callers

nothing calls this directly

Calls 2

FutureResultClass · 0.85
factoryFunction · 0.70

Tested by

no test coverage detected