Question regarding awaitables
See original GitHub issueHi @maxfischer2781 , First of all, thanks for developing asyncstdlib!
I am writing icontract, a library for design-by-contract in Python. As Python 3 lacks the support for async lambdas (see the Python issue 33447) I am searching for a workaround so that contracts can still be defined as lambda functions. Instead of a concrete (realized) value, the lambda returns a coroutine. I am wondering whether asyncstdlib can fit the bill?
I played with it in the console, but couldn’t really figure out how to achieve what I need. Here is a short code snippet defining functions that I’d need:
T = TypeVar('T')
async def awaited_all(aws: Iterable[Awaitable[T]]) -> bool:
"""Await each item in ``aws`` and then check that ``all`` holds."""
for awaitable in aws:
if not await awaitable:
return False
return True
U = TypeVar('U')
V = TypeVar('V')
async def awaited_to(func: Callable[[U], V], awaitable: Awaitable[U]) -> V:
"""Await the ``awaitable`` and then apply ``func`` on it."""
return func(await awaitable)
As you see, the implementation is quite simple. As far as I understood, the focus of asyncstdlib is on async iterators instead of sync iterators over awaitables, is that correct?
If asyncstdlib can not be used, are you maybe aware of a library that implements the functions that I need?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:12 (12 by maintainers)
Top GitHub Comments
No worries – brain storming is important 😃.
I would argue against complex implementations. First, because the library like this needs a high level of reliability (simple = “obviously no bugs, rather than no obvious bugs”). Second, this lib aims to be heavily used in many places throughout the system, so a small overhead accumulates and can cause a significant slow down.
Closing since this issue was forked into issues #28 and #33.