Repository navigation
Accessing streaming Response.encoding early disables callable auto-detection #3803
Replies: 1 comment
|
For callers affected by the early property access, a workaround is to run the detector explicitly after buffering, then assign the encoding before accessing # detect is the same bytes -> encoding callable passed to the client.
response.read() # await response.aread() for AsyncClient
response.encoding = detect(response.content) or "utf-8"
text = response.textTested on HTTPX 0.28.1 with If This is a caller-side workaround; the compatibility decision about changing the property's caching behavior still needs maintainer input. HTTPX encoding documentation. |
Uh oh!
There was an error while loading. Please reload this page.
Potential issue
For a streaming response with a callable
default_encoding, inspectingresponse.encodingbefore reading the body permanently caches a provisional UTF-8 fallback. The callable then never runs, and.textdepends on property-access order.Minimal reproduction
Actual:
Expected: once the response is read, the second response should call the detector and decode identically to the first. It seems reasonable to return a provisional UTF-8 fallback before bytes are available without caching it; on subsequent access after
.read()/.aread(), call the detector. This would not change incrementaliter_text()decoding (which cannot auto-detect without buffering).httpx/_models.py:652-672,Response.encoding, currently savesself._encoding = encoding or "utf-8"even when the body is not available. Is recalculating after reading in this case compatible with the intended property semantics? I can submit a focused PR with sync/async tests and documentation if approved.Verified against default-branch commit
b5addb64f0161ff6bfe94c124ef76f6a1fba5254, HTTPX 0.28.1, Python 3.12.13 on Windows; searched open/closed issues and PRs for streaming/autodetection/early.encodingaccess and found no matching report.AI disclosure: bug discovery assisted by Luna (
timotty-responses/luna); reproduction and proposed fix reviewed with OpenCode (GPT-6 Sol). No PR has been published.All reactions