Skip to content

Implement Response.stream_text() - #183

Merged
sethmlarson merged 11 commits into
encode:masterfrom
sethmlarson:text-stream
Aug 15, 2019
Merged

sethmlarson merged 11 commits into
encode:masterfrom
sethmlarson:text-stream

Conversation

@sethmlarson

@sethmlarson sethmlarson commented Aug 1, 2019 •

Copy link
Copy Markdown
Contributor

I use a separate method so that there's a definite type coming out of the function (typing.Iterator[str]) and would be impossible to determine the type of the iterator if we used the parameter route. Let me know what you think of that approach.

Closes #24

@lovelydinosaur

Copy link
Copy Markdown
Member

Coolio!

Okay, so it looks to me like this can roughly be characterised as:

  • Don't attempt to start decoding until we've got at least 512 bytes.
  • If we've got between 512-4096 bytes, then see if we've figured out a decoding and start using it if we have.
  • If we get to over 4096 bytes, the no more hanging around please - just start using whatever our best guess decoding is.

Have I got that right?

I think it'd be a good trade-off for us to switch to a simpler model here...

  • Don't attempt to start decoding until we've got at least 4096 bytes. (Or we're done and flushed)
  • Once we get there just start using whatever our best guess decoding is.

That'd give us more testable, and more determinate behavior, while still being an adequately small amount of initial buffering to be suitable for streamed data.

Seem reasonable?

@sethmlarson

Copy link
Copy Markdown
Contributor Author

Yeah that works, I'll change it to that.

@sethmlarson

Copy link
Copy Markdown
Contributor Author

This is ready for your review again @tomchristie

Comment thread httpx/decoders.py Outdated
result = self.detector.result["encoding"]

# I don't think that the second case is even possible for chardet to
# do in it's current implementation but just in case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As a rule I think we should probably prefer not to bother with "just in case", but deal with it if a bug report comes in. (At least then we have a verified case, and history to the code)

@sethmlarson
sethmlarson merged commit af907e8 into encode:master Aug 15, 2019
@sethmlarson
sethmlarson deleted the text-stream branch August 15, 2019 02:56
Kludex referenced this pull request in pydantic/httpx2 May 11, 2026
* Ignored a new error from updated flake8-pie

* Replaced all-encompassing ignore of PIE786 with specific noqa comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Text streaming

2 participants