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
Provenance
- Commit
8752e26- Tree
06f6e69- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
9f9deea
3 files changed+4 added−40 removed
modified:httpx/_content.py+2 added-6 removed
50 unmodified lines51 if self._is_stream_consumed and self._is_generator:52 raise StreamConsumed()53 54 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 chunk16 unmodified lines76 if self._is_stream_consumed and self._is_generator:77 raise StreamConsumed()78 79 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 chunk151 unmodified linesmodified:httpx/_types.py-32 removed
105 unmodified lines106 def close(self) -> None:107 """108 Subclasses can override this method to release any network resources109 after a request/response cycle is complete.110-111- Streaming cases should use a `try...finally` block to ensure that112- 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()110 """111 123- def read(self) -> bytes:124- """125- Simple cases can use `.read()` as a convenience method for consuming126- the entire stream and then closing it.112 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()137-138-113 class AsyncByteStream:114 async def __aiter__(self) -> AsyncIterator[bytes]:115 raise NotImplementedError(116 "The '__aiter__' method must be implemented."1 unmodified line118 yield b"" # pragma: nocover119 120 async def aclose(self) -> None:121 pass148-149- 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 lines12 headers, stream = encode_request()13 assert isinstance(stream, httpx.SyncByteStream)14 assert isinstance(stream, httpx.AsyncByteStream)15 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])18 19 assert headers == {}20 assert sync_content == b""21 assert async_content == b""429 unmodified lines