|
21 | 21 | Awaitable, |
22 | 22 | Callable, |
23 | 23 | Generator, |
| 24 | + Generic, |
24 | 25 | Iterable, |
25 | 26 | Optional, |
26 | 27 | TypeVar, |
@@ -279,58 +280,85 @@ async def fn_async(*args: P.args, **kwargs: P.kwargs) -> R: |
279 | 280 | return fn_async |
280 | 281 |
|
281 | 282 |
|
282 | | -# # TODO-barret-future; Q: Keep code? |
283 | | -# class WrapAsync(Generic[P, R]): |
284 | | -# """ |
285 | | -# Make a function asynchronous. |
286 | | - |
287 | | -# Parameters |
288 | | -# ---------- |
289 | | -# fn |
290 | | -# Function to make asynchronous. |
291 | | - |
292 | | -# Returns |
293 | | -# ------- |
294 | | -# : |
295 | | -# Asynchronous function (within the `WrapAsync` instance) |
296 | | -# """ |
297 | | - |
298 | | -# def __init__(self, fn: Callable[P, R] | Callable[P, Awaitable[R]]): |
299 | | -# if isinstance(fn, WrapAsync): |
300 | | -# fn = cast(WrapAsync[P, R], fn) |
301 | | -# return fn |
302 | | -# self._is_async = is_async_callable(fn) |
303 | | -# self._fn = wrap_async(fn) |
304 | | - |
305 | | -# async def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: |
306 | | -# """ |
307 | | -# Call the asynchronous function. |
308 | | -# """ |
309 | | -# return await self._fn(*args, **kwargs) |
310 | | - |
311 | | -# @property |
312 | | -# def is_async(self) -> bool: |
313 | | -# """ |
314 | | -# Was the original function asynchronous? |
315 | | - |
316 | | -# Returns |
317 | | -# ------- |
318 | | -# : |
319 | | -# Whether the original function is asynchronous. |
320 | | -# """ |
321 | | -# return self._is_async |
322 | | - |
323 | | -# @property |
324 | | -# def fn(self) -> Callable[P, R] | Callable[P, Awaitable[R]]: |
325 | | -# """ |
326 | | -# Retrieve the original function |
327 | | - |
328 | | -# Returns |
329 | | -# ------- |
330 | | -# : |
331 | | -# Original function supplied to the `WrapAsync` constructor. |
332 | | -# """ |
333 | | -# return self._fn |
| 283 | +class WrapAsync(Generic[P, R]): |
| 284 | + """ |
| 285 | + Make a function asynchronous. |
| 286 | +
|
| 287 | + Parameters |
| 288 | + ---------- |
| 289 | + fn |
| 290 | + Function to make asynchronous. |
| 291 | +
|
| 292 | + Returns |
| 293 | + ------- |
| 294 | + : |
| 295 | + Asynchronous function (within the `WrapAsync` instance) |
| 296 | + """ |
| 297 | + |
| 298 | + _fn: Callable[P, Awaitable[R]] |
| 299 | + _is_async: bool |
| 300 | + _orig_fn: Callable[P, R] | Callable[P, Awaitable[R]] |
| 301 | + |
| 302 | + def __init__( |
| 303 | + self, |
| 304 | + fn: Callable[P, R] | Callable[P, Awaitable[R]], |
| 305 | + ): |
| 306 | + if isinstance(fn, WrapAsync): |
| 307 | + wa = cast(WrapAsync[P, R], fn) |
| 308 | + self._fn = wa._fn |
| 309 | + self._is_async = wa._is_async |
| 310 | + self._orig_fn = wa._orig_fn |
| 311 | + else: |
| 312 | + self._is_async = is_async_callable(fn) |
| 313 | + self._fn = wrap_async(fn) |
| 314 | + self._orig_fn = fn |
| 315 | + |
| 316 | + async def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: |
| 317 | + """ |
| 318 | + Call the asynchronous function. |
| 319 | + """ |
| 320 | + return await self._fn(*args, **kwargs) |
| 321 | + |
| 322 | + def is_async(self) -> bool: |
| 323 | + """ |
| 324 | + Was the original function asynchronous? |
| 325 | +
|
| 326 | + Returns |
| 327 | + ------- |
| 328 | + : |
| 329 | + Whether the original function is asynchronous. |
| 330 | + """ |
| 331 | + return self._is_async |
| 332 | + |
| 333 | + def get_async_fn(self) -> Callable[P, Awaitable[R]]: |
| 334 | + """ |
| 335 | + Return the async value function. |
| 336 | +
|
| 337 | + Returns |
| 338 | + ------- |
| 339 | + : |
| 340 | + Async wrapped value function supplied to the `AsyncValueFn` constructor. |
| 341 | + """ |
| 342 | + return self._fn |
| 343 | + |
| 344 | + def get_sync_fn(self) -> Callable[P, R]: |
| 345 | + """ |
| 346 | + Retrieve the original, synchronous value function function. |
| 347 | +
|
| 348 | + If the original function was asynchronous, a runtime error will be thrown. |
| 349 | +
|
| 350 | + Returns |
| 351 | + ------- |
| 352 | + : |
| 353 | + Original, synchronous function supplied to the `AsyncValueFn` constructor. |
| 354 | + """ |
| 355 | + if self._is_async: |
| 356 | + raise RuntimeError( |
| 357 | + "The original function was asynchronous. Use `async_fn` instead." |
| 358 | + ) |
| 359 | + |
| 360 | + sync_fn = cast(Callable[P, R], self._orig_fn) |
| 361 | + return sync_fn |
334 | 362 |
|
335 | 363 |
|
336 | 364 | # This function should generally be used in this code base instead of |
|
0 commit comments