Beispiel #1
0
    async def test_on_genexp(self):
        async def genexp():
            yield ...
            yield ...

        exp = genexp()
        try:
            assert not aio.is_async_iterator(exp)
        finally:
            await exp.aclose()
Beispiel #2
0
    async def _wrap_iter(self) -> typing.AsyncGenerator[typing.Any, bytes]:
        if isinstance(self.data, bytes):
            for i in range(0, len(self.data), _MAGIC):
                yield self.data[i:i + _MAGIC]  # noqa: E203

        elif aio.is_async_iterator(self.data) or inspect.isasyncgen(self.data):
            try:
                while True:
                    yield self._assert_bytes(
                        await
                        self.data.__anext__())  # type: ignore[union-attr]
            except StopAsyncIteration:
                pass

        elif isinstance(self.data, typing.Iterator):
            try:
                while True:
                    yield self._assert_bytes(next(self.data))
            except StopIteration:
                pass

        elif inspect.isgenerator(self.data):
            try:
                while True:
                    yield self._assert_bytes(
                        self.data.send(None))  # type: ignore[union-attr]
            except StopIteration:
                pass

        elif aio.is_async_iterable(self.data):
            async for chunk in self.data:  # type: ignore[union-attr]
                yield self._assert_bytes(chunk)

        elif isinstance(self.data, typing.Iterable):
            for chunk in self.data:
                yield self._assert_bytes(chunk)

        else:
            # Will always fail.
            self._assert_bytes(self.data)
Beispiel #3
0
    def test_on_class(self):
        class AsyncIterator:
            async def __anext__(self):
                return ...

        assert aio.is_async_iterator(AsyncIterator)
Beispiel #4
0
    def test_on_inst(self):
        class AsyncIterator:
            async def __anext__(self):
                return None

        assert aio.is_async_iterator(AsyncIterator())
Beispiel #5
0
    def test_on_async_iterable_class(self):
        class AsyncIter:
            def __aiter__(self):
                yield ...

        assert not aio.is_async_iterator(AsyncIter)
Beispiel #6
0
    def test_on_iterator_class(self):
        class Iter:
            def __next__(self):
                return ...

        assert not aio.is_async_iterator(Iter)