Repository navigation
Conversation
|
All CI passing on Python 3.9-3.13, ready for merge |
|
Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed. |
When AsyncClient receives invalid data like data=[{"a": "b"}] (list of dicts),
it was raising "Attempted to send a sync request with an AsyncClient instance"
which is misleading. The actual issue is invalid data format.
This fix adds early validation in encode_request() to check if data is a
list/tuple containing non-bytes objects, and raises a clear TypeError with
helpful guidance (use json= or data={...} instead).
Fixes encode#3471
Remove unnecessary try-except block that was catching TypeError during iteration. The validation logic doesn't need exception handling since we're only checking types of items in a list/tuple that we've already confirmed exists.
5a2b188 to
843346f
Compare
OKAN-SOFTWARE
left a comment
There was a problem hiding this comment.
I noticed two related gaps in the regression coverage.
The new validation checks list and tuple, but other iterables can still bypass it. For example, passing data=iter([{"a": "b"}]) proceeds through encode_content() and creates an IteratorByteStream. With AsyncClient, this appears to preserve the original generic error about sending a synchronous request.
Also, test_invalid_data_list_of_dicts_async() currently constructs an httpx.Request directly. Although the test is declared asynchronous, it never exercises AsyncClient, which is the behavior reported in #3471.
Could we add a regression test that actually calls AsyncClient.post() with invalid data, including a non-list iterable?
This would verify that the improved diagnostic is raised through the original failure path and help prevent the same issue from surviving for other iterable inputs.
Summary
Fixes #3471
When
AsyncClientreceives invaliddatalikedata=[{"a": "b"}](a list of dicts), it was raising a confusing error:This error is misleading because the user IS using an async client correctly - the actual problem is passing invalid data.
With this fix, both sync and async clients now raise a clear error immediately:
Changes
encode_request()to check ifdatais a list/tuple containing non-bytes objectsTypeErrorwith helpful error message directing users to correct alternatives (json=ordata={...})Test Plan
Before this fix:
After this fix:
Valid use cases still work:
data={"key": "value"}(dict for form encoding)data=[b"bytes", b"data"](list of bytes)json={"key": "value"}(JSON encoding)Testing
tests/test_content.py