Skip to content

Commit

Permalink
Fix AsyncResponse.iter_lines() (#182)
Browse files Browse the repository at this point in the history
Doesn't work now. Causes an exception:`TypeError: 'async for' requires
an object with __aiter__ method, got coroutine`. MRE:

```python
import asyncio

import niquests


async def main() -> None:
    async with niquests.AsyncSession() as session:
        response: niquests.AsyncResponse = session.post(..., stream=True)
        async for line in response.iter_lines():
            print(line)


asyncio.run(main())

```

---------

Co-authored-by: Ahmed TAHRI <[email protected]>
  • Loading branch information
vrslev and Ousret authored Nov 22, 2024
1 parent dd70717 commit 29c9c98
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 26 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Release History
===============

3.11.1 (2024-11-22)
-------------------

**Fixed**
- async version of ``iter_line``. (#182)

3.11.0 (2024-11-20)
-------------------

Expand Down
16 changes: 16 additions & 0 deletions docs/user/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,22 @@ Delaying the content consumption in an async context can be easily achieved usin

asyncio.run(main())

Or using the ``iter_line`` method as such::

import niquests
import asyncio

async def main() -> None:

async with niquests.AsyncSession() as s:
r = await s.get("https://pie.dev/get", stream=True)

async for chunk in r.iter_line():
print(chunk)

if __name__ == "__main__":
asyncio.run(main())

Or simply by doing::

import niquests
Expand Down
4 changes: 2 additions & 2 deletions src/niquests/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
__url__: str = "https://niquests.readthedocs.io"

__version__: str
__version__ = "3.11.0"
__version__ = "3.11.1"

__build__: int = 0x031100
__build__: int = 0x031101
__author__: str = "Kenneth Reitz"
__author_email__: str = "[email protected]"
__license__: str = "Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions src/niquests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ async def iter_lines( # type: ignore[misc]

pending = None

async for chunk in self.iter_content( # type: ignore[call-overload]
async for chunk in await self.iter_content( # type: ignore[call-overload]
chunk_size=chunk_size, decode_unicode=decode_unicode
):
if pending is not None:
Expand All @@ -1816,7 +1816,7 @@ async def iter_lines( # type: ignore[misc]
else:
pending = None

async for line in lines:
for line in lines:
yield line

if pending is not None:
Expand Down
45 changes: 23 additions & 22 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,29 @@ async def callback_on_early(early_resp) -> None:
assert resp.status_code == 200
assert received_early_response is True

# async def test_http_trailer_preload(self) -> None:
# async with AsyncSession() as s:
# r = await s.get("https://httpbingo.org/trailers?foo=baz")
#
# assert r.ok
# assert r.trailers
# assert "foo" in r.trailers
# assert r.trailers["foo"] == "baz"
#
# async def test_http_trailer_no_preload(self) -> None:
# async with AsyncSession() as s:
# r = await s.get("https://httpbingo.org/trailers?foo=baz", stream=True)
#
# assert r.ok
# assert not r.trailers
# assert "foo" not in r.trailers
#
# await r.content
#
# assert r.trailers
# assert "foo" in r.trailers
# assert r.trailers["foo"] == "baz"
async def test_iter_line(self) -> None:
async with AsyncSession() as s:
r = await s.get("https://httpbingo.org/html", stream=True)
content = b""

async for line in r.iter_lines():
assert isinstance(line, bytes)
content += line

assert content
assert b"Herman Melville - Moby-Dick" in content

async def test_iter_line_decode(self) -> None:
async with AsyncSession() as s:
r = await s.get("https://httpbingo.org/html", stream=True)
content = ""

async for line in r.iter_lines(decode_unicode=True):
assert isinstance(line, str)
content += line

assert content
assert "Herman Melville - Moby-Dick" in content


@pytest.mark.usefixtures("requires_wan")
Expand Down

0 comments on commit 29c9c98

Please sign in to comment.