74
74
str , Sequence [Union [str , Mapping [str , Union [str , int ]], NodeConfig ]]
75
75
]
76
76
77
+ _TYPE_BODY = Union [bytes , str , Dict [str , Any ]]
78
+
77
79
_TYPE_ASYNC_SNIFF_CALLBACK = Callable [
78
80
[AsyncTransport , SniffOptions ], Awaitable [List [NodeConfig ]]
79
81
]
@@ -295,15 +297,21 @@ def _merge_kwargs_no_duplicates(kwargs: Dict[str, Any], values: Dict[str, Any])
295
297
296
298
297
299
def _merge_body_fields_no_duplicates (
298
- body : Dict [ str , Any ] , kwargs : Dict [str , Any ], body_fields : Tuple [str , ...]
300
+ body : _TYPE_BODY , kwargs : Dict [str , Any ], body_fields : Tuple [str , ...]
299
301
) -> None :
300
302
for key in list (kwargs .keys ()):
301
- if key in body :
302
- raise ValueError (
303
- f"Received multiple values for '{ key } ', specify parameters "
304
- "using either body or parameters, not both."
305
- )
306
303
if key in body_fields :
304
+ if isinstance (body , (str , bytes )):
305
+ raise ValueError (
306
+ "Couldn't merge 'body' with other parameters as it wasn't a mapping."
307
+ )
308
+
309
+ if key in body :
310
+ raise ValueError (
311
+ f"Received multiple values for '{ key } ', specify parameters "
312
+ "using either body or parameters, not both."
313
+ )
314
+
307
315
warnings .warn (
308
316
f"Received '{ key } ' via a specific parameter in the presence of a "
309
317
"'body' parameter, which is deprecated and will be removed in a future "
@@ -392,7 +400,7 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
392
400
if "body" in kwargs and (
393
401
not ignore_deprecated_options or "body" not in ignore_deprecated_options
394
402
):
395
- body = kwargs .pop ("body" )
403
+ body : Optional [ _TYPE_BODY ] = kwargs .pop ("body" )
396
404
if body is not None :
397
405
if body_name :
398
406
if body_name in kwargs :
@@ -405,11 +413,6 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
405
413
kwargs [body_name ] = body
406
414
407
415
elif body_fields is not None :
408
- if not hasattr (body , "items" ):
409
- raise ValueError (
410
- "Couldn't merge 'body' with other parameters as it wasn't a mapping. "
411
- "Instead of using 'body' use individual API parameters"
412
- )
413
416
_merge_body_fields_no_duplicates (body , kwargs , body_fields )
414
417
kwargs ["body" ] = body
415
418
0 commit comments