vxgmichel / aiostream

Generator-based operators for asynchronous iteration
http://aiostream.readthedocs.io
GNU General Public License v3.0
801 stars 34 forks source link

Have a better exception than AttributeError when a stream is used as a context #40

Closed jonathon-love closed 4 years ago

jonathon-love commented 5 years ago

hi i can use enumerate() improperly and it works as expected, i.e.:

async for i, r in enumerate(analysis.results):
    print(r)

but of course i get the UserWarning: AsyncIteratorContext is iterated outside of its context

but when i try and do it properly:

async with enumerate(analysis.results) as s:
    async for i, r in s:
        print(r)

i get an AttributeError: __aexit__ on the with line.

i assume this is a bug (or am i misunderstanding something)?

with thanks

vxgmichel commented 5 years ago

Hi @jonathon-love, sorry for the late reply!

The issue here is that you forgot to use the stream method:

xs = stream.enumerate(analysis.results)
async with xs.stream() as s:
    async for i, r in s:
        print(r)

I know it is a bit confusing, but streams can be iterated multiple times so a new streamer object has to be created for each usage. I think we need at least a better exception message than AttributeError: __aexit__ so people don't get too confused about it. I'll rename the issue.

Thanks for the report!

jonathon-love commented 5 years ago

ah! my bad!

thanks for the response.

vxgmichel commented 4 years ago

Fixed! Thanks again for the report @jonathon-love