-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature suggestion: strict
parameter for zip
#118
Comments
Hi @smheidrich, thanks for the suggestion 👍 I think it would be useful too, feel free to create a PR if you feel like contributing to the project. The implementation is over there: aiostream/aiostream/stream/combine.py Lines 49 to 86 in 5e0547a
It relies on asyncio.gather which exposes a async_stop_iteration_sentinel = object()
if strict:
async def next_item(streamer):
try:
return await anext(streamer)
except StopAsyncIteration:
return async_stop_iteration_sentinel
else:
next_item = anext
[...]
try:
coros = builtins.map(next_item, streamers)
items = await asyncio.gather(*coros)
except StopAsyncIteration:
break
if strict:
if all(item == async_stop_iteration_sentinel for item in items):
break
if any(item == async_stop_iteration_sentinel for item in items):
raise ValueError
[...] Otherwise, I'll do it myself when I find the time :) |
* Add strict parameter to stream.zip (issue #118) * Use shortcut for anext called without default * Add tests for exception passthrough in zip * Add (failing) test case for early exit from zip * Exit from non-strict zip as early as possible Fixes failing test from previous commit. * Make UNSET an enum Co-authored-by: Vincent Michel <[email protected]> * Make STOP_SENTINEL an enum Co-authored-by: Vincent Michel <[email protected]> * Fix imports for enums * Move strict condition further up & fix typing * Update aiostream/stream/combine.py Fix Pyton 3.8 compat Co-authored-by: Vincent Michel <[email protected]> * Move STOP_SENTINEL construction out of function * Type inner anext wrapper function * Improve un-overloaded anext() type signature * Use ellipsis instead of pass --------- Co-authored-by: Vincent Michel <[email protected]>
Done and released in v0.6.2 🎉 Thanks a lot @smheidrich for your valuable contribution ! |
Great!! Thanks for the reviews & merge! 👍 |
Python 3.10 introduced the
strict
parameter for the built-inzip
function, which, when set toTrue
, can help spot bugs by raising an exception when the supplied iterables turn out to have different lengths (i.e. when one raisesStopIteration
after fewer iterations than the others).I think it would be useful if aiostream's
zip
function supported this parameter as well.Extra context: The
strict
parameter of Python's built-inzip
turned out to be useful enough that linters like Ruff or flake8 with the bugbear plugin even have a rule to complain when you neither use it nor explicitly disable it by settingstrict=False
.The text was updated successfully, but these errors were encountered: