Fix: Raise helpful TypeError for invalid list 'data'#3625
Fix: Raise helpful TypeError for invalid list 'data'#3625mahdirahimi1999 wants to merge 4 commits intoencode:masterfrom mahdirahimi1999:fix-data-list-typeerror
Conversation
Zaczero
left a comment
There was a problem hiding this comment.
P.S. I am just a random user browsing through open PRs; I may be completely wrong.
| if data and all( | ||
| isinstance(item, (dict, str, int, float, bool)) for item in data | ||
| ): |
There was a problem hiding this comment.
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]]] |
There was a problem hiding this comment.
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.
|
Thanks tho no. Note that the upcoming |
Fixes #3471
Summary
This PR fixes a misleading error that occurred when passing an invalid list (e.g., a list of dictionaries) to the
dataparameter.Previously,
AsyncClientwould raise a confusingRuntimeErrorinstead of aTypeErrorthat accurately described the problem. This change introduces a validation check inBaseClientto catch this common mistake early and raises a helpfulTypeError. The new error message guides the user to use thejsonparameter for JSON arrays, ensuring consistent behavior between both sync and async clients.Checklist