Skip to content

Commit 8752e26

Browse files
Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)
1 parent 9f9deea commit 8752e26

3 files changed

Lines changed: 4 additions & 40 deletions

File tree

httpx/_content.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ def __iter__(self) -> Iterator[bytes]:
5252
raise StreamConsumed()
5353

5454
self._is_stream_consumed = True
55-
if hasattr(self._stream, "read") and not isinstance(
56-
self._stream, SyncByteStream
57-
):
55+
if hasattr(self._stream, "read"):
5856
# File-like interfaces should use 'read' directly.
5957
chunk = self._stream.read(self.CHUNK_SIZE) # type: ignore
6058
while chunk:
@@ -79,9 +77,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
7977
raise StreamConsumed()
8078

8179
self._is_stream_consumed = True
82-
if hasattr(self._stream, "aread") and not isinstance(
83-
self._stream, AsyncByteStream
84-
):
80+
if hasattr(self._stream, "aread"):
8581
# File-like interfaces should use 'aread' directly.
8682
chunk = await self._stream.aread(self.CHUNK_SIZE) # type: ignore
8783
while chunk:

httpx/_types.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -107,33 +107,7 @@ def close(self) -> None:
107107
"""
108108
Subclasses can override this method to release any network resources
109109
after a request/response cycle is complete.
110-
111-
Streaming cases should use a `try...finally` block to ensure that
112-
the stream `close()` method is always called.
113-
114-
Example:
115-
116-
status_code, headers, stream, extensions = transport.handle_request(...)
117-
try:
118-
...
119-
finally:
120-
stream.close()
121-
"""
122-
123-
def read(self) -> bytes:
124110
"""
125-
Simple cases can use `.read()` as a convenience method for consuming
126-
the entire stream and then closing it.
127-
128-
Example:
129-
130-
status_code, headers, stream, extensions = transport.handle_request(...)
131-
body = stream.read()
132-
"""
133-
try:
134-
return b"".join([part for part in self])
135-
finally:
136-
self.close()
137111

138112

139113
class AsyncByteStream:
@@ -145,9 +119,3 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
145119

146120
async def aclose(self) -> None:
147121
pass
148-
149-
async def aread(self) -> bytes:
150-
try:
151-
return b"".join([part async for part in self])
152-
finally:
153-
await self.aclose()

tests/test_content.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ async def test_empty_content():
1313
assert isinstance(stream, httpx.SyncByteStream)
1414
assert isinstance(stream, httpx.AsyncByteStream)
1515

16-
sync_content = stream.read()
17-
async_content = await stream.aread()
16+
sync_content = b"".join([part for part in stream])
17+
async_content = b"".join([part async for part in stream])
1818

1919
assert headers == {}
2020
assert sync_content == b""

0 commit comments

Comments
 (0)