From cc87a2e10d77e71ea517999b75369a4280d81de0 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 13 Oct 2022 14:02:28 +0100 Subject: [PATCH] Drop .read/.aread from SyncByteStream/AsyncByteStream --- httpx/_content.py | 8 ++------ httpx/_types.py | 32 -------------------------------- tests/test_content.py | 4 ++-- 3 files changed, 4 insertions(+), 40 deletions(-) diff --git a/httpx/_content.py b/httpx/_content.py index 1f149a19ce..9b86541dfa 100644 --- a/httpx/_content.py +++ b/httpx/_content.py @@ -52,9 +52,7 @@ def __iter__(self) -> Iterator[bytes]: raise StreamConsumed() self._is_stream_consumed = True - if hasattr(self._stream, "read") and not isinstance( - self._stream, SyncByteStream - ): + if hasattr(self._stream, "read"): # File-like interfaces should use 'read' directly. chunk = self._stream.read(self.CHUNK_SIZE) # type: ignore while chunk: @@ -79,9 +77,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]: raise StreamConsumed() self._is_stream_consumed = True - if hasattr(self._stream, "aread") and not isinstance( - self._stream, AsyncByteStream - ): + if hasattr(self._stream, "aread"): # File-like interfaces should use 'aread' directly. chunk = await self._stream.aread(self.CHUNK_SIZE) # type: ignore while chunk: diff --git a/httpx/_types.py b/httpx/_types.py index 8099f7b4dd..08fee30b7d 100644 --- a/httpx/_types.py +++ b/httpx/_types.py @@ -107,33 +107,7 @@ def close(self) -> None: """ Subclasses can override this method to release any network resources after a request/response cycle is complete. - - Streaming cases should use a `try...finally` block to ensure that - the stream `close()` method is always called. - - Example: - - status_code, headers, stream, extensions = transport.handle_request(...) - try: - ... - finally: - stream.close() - """ - - def read(self) -> bytes: """ - Simple cases can use `.read()` as a convenience method for consuming - the entire stream and then closing it. - - Example: - - status_code, headers, stream, extensions = transport.handle_request(...) - body = stream.read() - """ - try: - return b"".join([part for part in self]) - finally: - self.close() class AsyncByteStream: @@ -145,9 +119,3 @@ async def __aiter__(self) -> AsyncIterator[bytes]: async def aclose(self) -> None: pass - - async def aread(self) -> bytes: - try: - return b"".join([part async for part in self]) - finally: - await self.aclose() diff --git a/tests/test_content.py b/tests/test_content.py index afd910399e..b79749bb92 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -13,8 +13,8 @@ async def test_empty_content(): assert isinstance(stream, httpx.SyncByteStream) assert isinstance(stream, httpx.AsyncByteStream) - sync_content = stream.read() - async_content = await stream.aread() + sync_content = b"".join([part for part in stream]) + async_content = b"".join([part async for part in stream]) assert headers == {} assert sync_content == b""