Repository navigation
Await coroutine functions in concatmap/flatmap/switchmap (#129) - #140
Closed
apoorvdarshan wants to merge 1 commit into
Closed
apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
The advanced *map operators built the intermediate stream via `combine.smap.raw`, the synchronous map that never awaits its function. When passed an `async def` function, `smap` yielded the un-awaited coroutine object downstream, which then failed the `assert isinstance(result, AsyncIterable)` check in `base_combine` (and emitted a "coroutine was never awaited" RuntimeWarning). Route through `combine.map.raw` when the function is a coroutine function (detected with `asyncio.iscoroutinefunction`, as `combine.map` already does), and keep using `combine.smap.raw` otherwise. This mirrors the existing behaviour of `stream.map` and awaits the coroutine before its result is iterated. Fixes vxgmichel#129
Owner
|
Hi @apoorvdarshan, thank you for taking the time to make this PR :) It turns out that the fix you presented does not respect the However a proper fix exists, as I discovered while replying in #129. Would you like to integrate it to your PR? Otherwise, I'll create a new one myself, you let me know :) |
Owner
|
I went ahead and merged #143, thanks for the PR :) |
Owner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #129
Root cause
concatmap,flatmapandswitchmapbuild their intermediate stream viacombine.smap.raw— the synchronous map that yieldsfunc(*item)withoutever awaiting it:
When
funcis anasync def,func(*item)returns a coroutine object ratherthan an
AsyncIterable. That coroutine is passed downstream tobase_combine,where it trips
assert isinstance(result, AsyncIterable)inaiostream/stream/advanced.py, and aRuntimeWarning: coroutine '...' was never awaitedis emitted.stream.mapalready handles this case by detectinga coroutine function with
asyncio.iscoroutinefunctionand routing to theasync path; the advanced
*mapoperators did not.Reproduction
Actual output before the fix:
Output after the fix (matching the equivalent synchronous function, which was
already supported):
Fix
Added a small
_map_sourceshelper inaiostream/stream/advanced.pythatroutes through
combine.map.rawwhen the function is a coroutine function(detected with
asyncio.iscoroutinefunction, the same idiomcombine.mapuses) and keeps using
combine.smap.rawotherwise. All three advanced*mapoperators now go through this helper, and their
funcannotation is broadenedfrom
SmapCallabletoMapCallableto reflect that a coroutine function isnow accepted (again mirroring
stream.map).The change is localized: the synchronous path is byte-for-byte equivalent to
the previous behaviour, and the async path reuses the already-tested
map/amapmachinery, so the coroutine is awaited before itsAsyncIterableresult is iterated. The
task_limitargument is intentionally left to governthe outer
flatten/concat/switchstage, exactly as before.Tests
Added
test_concatmap_with_coroutine_function,test_flatmap_with_coroutine_functionandtest_switchmap_with_coroutine_functionintests/test_advanced.py. Eachpasses an
async deffunction to the operator and asserts the flattenedoutput. All three fail on the unmodified code (with the "coroutine was never
awaited" warning) and pass with the fix. The full existing suite continues to
pass (
112 passed), andblack,ruff,flake8,mypy --strictandpyrightare clean on the changed files.Disclosure: prepared with AI assistance; reviewed and verified locally.