]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)
authorTom Christie <tom@tomchristie.com>
Mon, 7 Nov 2022 14:01:29 +0000 (14:01 +0000)
committerGitHub <noreply@github.com>
Mon, 7 Nov 2022 14:01:29 +0000 (14:01 +0000)
httpx/_content.py
httpx/_types.py
tests/test_content.py

index 1f149a19ceade923c8d7d0be444e50dfa7fcb8dd..9b86541dfab85c22c74484106b3b1eed9e350292 100644 (file)
@@ -52,9 +52,7 @@ class IteratorByteStream(SyncByteStream):
             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 @@ class AsyncIteratorByteStream(AsyncByteStream):
             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:
index 8099f7b4dd8de1dc4e0e036c55d2f4fa46d1c35f..08fee30b7da59945d6dad87d09c26c7a3086d194 100644 (file)
@@ -107,33 +107,7 @@ class SyncByteStream:
         """
         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 @@ class AsyncByteStream:
 
     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()
index afd910399ededd14babcf4084201ed324fd95c96..b79749bb92e09479a69106b139866e88d50de4b5 100644 (file)
@@ -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""