Skip to content

Conversation

@mahdirahimi1999
Copy link

@mahdirahimi1999 mahdirahimi1999 commented Aug 12, 2025

Fixes #3471

Summary

This PR fixes a misleading error that occurred when passing an invalid list (e.g., a list of dictionaries) to the data parameter.

Previously, AsyncClient would raise a confusing RuntimeError instead of a TypeError that accurately described the problem. This change introduces a validation check in BaseClient to catch this common mistake early and raises a helpful TypeError. The new error message guides the user to use the json parameter for JSON arrays, ensuring consistent behavior between both sync and async clients.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly. (N/A: This change improves an error message and does not require a documentation update.)

Copy link
Contributor

@Zaczero Zaczero left a comment

Choose a reason for hiding this comment

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

P.S. I am just a random user browsing through open PRs; I may be completely wrong.

Comment on lines +370 to +372
if data and all(
isinstance(item, (dict, str, int, float, bool)) for item in data
):
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems quite inefficient. Also, logically it should be the inverse as you want to check for 2-item collections: For form data, use a dictionary or a list of 2-item. So whitelist instead of blacklist. For example, list[None] is not raising a TypeError here, and much more.

ResponseExtensions = Mapping[str, Any]

RequestData = Mapping[str, Any]
RequestData = Union[Mapping[str, Any], List[Tuple[str, Any]]]
Copy link
Contributor

Choose a reason for hiding this comment

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

The PR says: Raise helpful TypeError for invalid list 'data', but you seem to add a brand new set of logic for handling new request data type.

@lovelydinosaur
Copy link
Contributor

Thanks tho no. Note that the upcoming httpx 1.0 pre-release comprehensively deals with getting to a much cleaner & more minimally typed API.

@mahdirahimi1999 mahdirahimi1999 deleted the fix-data-list-typeerror branch September 5, 2025 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Raising incorrect error when passing array to data

3 participants