Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Nov 7, 2022

Conversation

tomchristie
Copy link
Member

We have a leftover bit of API on SyncByteStream/AsyncByteStream that is a hangover from when we were designing the Transport API.

They both have a convenience method for reading the stream, which was added to make testing at the low-level of the Transport API easier. However the methods are a bit kludgy because they don't actually fit the read(max_bytes) that Python file interfaces use.

I think we can just hard-drop these. They're undocumented, and you don't ever need them. The convenience methods are on the Response class.

@tomchristie tomchristie added the refactor Issues and PRs related to code refactoring label Oct 13, 2022
@@ -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()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're matching the style of the other test cases now.
These .read()/.aread() operations were only added to this test case so that we'd have coverage of those code lines.

@@ -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
):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The and not isinstance(self._stream, SyncByteStream) is what prompted me to clean these up.

It's such an oddly confusing branch - I couldn't figure out why it existed.

"Treat file-like objects as file-like objects, except for this one specific case".

No thanks.

Comment on lines -116 to -120
status_code, headers, stream, extensions = transport.handle_request(...)
try:
...
finally:
stream.close()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example from the old-style Transport API.

We had .read() so that users could eg. stream.read() here.

The Transport API now just returns fully fledged responses, so this is redundant.

@tomchristie
Copy link
Member Author

Thanks for the review @michaeloliverx.
Anyone want to merge this?...

@tomchristie
Copy link
Member Author

...How should we handle that generally?
Default to "pull request owner" can merge once approved?

@michaeloliverx
Copy link
Member

...How should we handle that generally? Default to "pull request owner" can merge once approved?

I think this is okay, maybe if you are personally reviewing something you can go ahead and merge it yourself.

@tomchristie tomchristie merged commit 8752e26 into master Nov 7, 2022
@tomchristie tomchristie deleted the drop-stream-read-method branch November 7, 2022 14:01
@tomchristie tomchristie mentioned this pull request Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Issues and PRs related to code refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants