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
{{ message }}
Repository navigation
Provide common replacement utils / functions many projects will need #65
defazip(*iterables): # Convert any iterables and async iterables into a zipped async iterableiterators= [aiter(x) forxiniterables]
asyncdefgenerator():
whileTrue:
try:
yieldtuple([awaitx.__anext__() forxiniterators])
exceptStopAsyncIteration:
breakreturngenerator().__aiter__()
defaiter(x): # Convert any iterable or async iterable into an async iteratorifhasattr(x, "__aiter__"):
returnx.__aiter__()
elifhasattr(x, "__anext__"):
returnxasyncdefaiter_wrapper():
foriteminx:
yielditemreturnaiter_wrapper().__aiter__()
asyncdefanext(i): # Advance an async iterator, good for mapping to `next()`returnawaiti.__anext__()
asyncdefawait_or_return(x): # Maps to 'return_()'?ifiscoroutine(x):
returnawaitxreturnx# All of the sync variations of the above functionsnext=nextiter=iterzip=zipdefreturn_(x):
returnx
Could also have azip_longest -> zip_longest, etc.
All of the async functions could be available only on Python 3.6+ so this could be used on projects that support 3.5 or less.
Could also provide type hints that are effected by unasync properly as well.
This should probably be a separate project to unasync as unasync is primarily a build tool, raising it here because it'd make sense for the tool to live under python-trio, maybe unasync-utils?
I'm a bit surprised here because my understanding is that unasync exists specifically so that you don't need functions like await_or_return. Instead return await x in the async version of a package becomes return x in the sync version.
I'm imaging a situation where your API takes a callable from the user and you'd like to support both Union[Callable[[...], X], Callable[[...], Awaitable[X]]] on the async implementation but only support Callable[[...], X] on the sync side. To do that you'd need a conditional await I think?
Maybe await_if_coro() or something like this is a better name for this interface?
An example where this would be useful in Hip: When given a body that has read() we want to call await f.read() for async files and f.read() on sync files.
Right, this is needed when your API accepts both async and sync callables. Even though unasync is only a build dependency, I don't really mind adding utilities like that and making it useful as a runtime dependency too
Activity
pquentin commentedon May 8, 2020
Thanks for opening this issue!
I'm a bit surprised here because my understanding is that unasync exists specifically so that you don't need functions like
await_or_return. Insteadreturn await xin the async version of a package becomesreturn xin the sync version.But since you know that, what am I missing here?
sethmlarson commentedon May 8, 2020
I'm imaging a situation where your API takes a callable from the user and you'd like to support both
Union[Callable[[...], X], Callable[[...], Awaitable[X]]]on the async implementation but only supportCallable[[...], X]on the sync side. To do that you'd need a conditional await I think?Maybe
await_if_coro()or something like this is a better name for this interface?sethmlarson commentedon May 9, 2020
An example where this would be useful in Hip: When given a body that has
read()we want to callawait f.read()for async files andf.read()on sync files.sethmlarson commentedon May 9, 2020
Also
anext()would be useful in a similar place in Hip for generating the async iterators of bytes to send.pquentin commentedon May 19, 2020
Right, this is needed when your API accepts both async and sync callables. Even though unasync is only a build dependency, I don't really mind adding utilities like that and making it useful as a runtime dependency too