Skip to content
Myr

Drop .read/.aread from SyncByteStream/AsyncByteStream (#2407)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent 9f9deea · Browse files · 3 files+4 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+4 added−40 removed

  • tests:+2 added−2 removed
  • other:+2 added−38 removed

3 files+4 added−40 removed

modified:httpx/_content.py+2 added-6 removed
50 unmodified lines
51 if self._is_stream_consumed and self._is_generator:52 raise StreamConsumed()5354 self._is_stream_consumed = True55 if hasattr(self._stream, "read") and not isinstance(56 self._stream, SyncByteStream57 ):55 if hasattr(self._stream, "read"):56 # File-like interfaces should use 'read' directly.57 chunk = self._stream.read(self.CHUNK_SIZE) # type: ignore58 while chunk:59 yield chunk
16 unmodified lines
76 if self._is_stream_consumed and self._is_generator:77 raise StreamConsumed()7879 self._is_stream_consumed = True82 if hasattr(self._stream, "aread") and not isinstance(83 self._stream, AsyncByteStream84 ):80 if hasattr(self._stream, "aread"):81 # File-like interfaces should use 'aread' directly.82 chunk = await self._stream.aread(self.CHUNK_SIZE) # type: ignore83 while chunk:84 yield chunk
151 unmodified lines
modified:httpx/_types.py-32 removed
105 unmodified lines
106 def close(self) -> None:107 """108 Subclasses can override this method to release any network resources109 after a request/response cycle is complete.110111 Streaming cases should use a `try...finally` block to ensure that112 the stream `close()` method is always called.113114 Example:115116 status_code, headers, stream, extensions = transport.handle_request(...)117 try:118 ...119 finally:120 stream.close()110 """111123 def read(self) -> bytes:124 """125 Simple cases can use `.read()` as a convenience method for consuming126 the entire stream and then closing it.112128 Example:129130 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()137138113class AsyncByteStream:114 async def __aiter__(self) -> AsyncIterator[bytes]:115 raise NotImplementedError(116 "The '__aiter__' method must be implemented."
1 unmodified line
118 yield b"" # pragma: nocover119120 async def aclose(self) -> None:121 pass148149 async def aread(self) -> bytes:150 try:151 return b"".join([part async for part in self])152 finally:153 await self.aclose()
modified:tests/test_content.py+2 added-2 removed
11 unmodified lines
12 headers, stream = encode_request()13 assert isinstance(stream, httpx.SyncByteStream)14 assert isinstance(stream, httpx.AsyncByteStream)1516 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])1819 assert headers == {}20 assert sync_content == b""21 assert async_content == b""
429 unmodified lines