File tree 3 files changed +27
-10
lines changed
3 files changed +27
-10
lines changed Original file line number Diff line number Diff line change @@ -70,17 +70,21 @@ def prepare_send_data(
70
70
if post_data is None :
71
71
post_data = {}
72
72
else :
73
- # booleans does not exists for data (neither for MultipartEncoder):
74
- # cast to string int to avoid: 'bool' object has no attribute 'encode'
73
+ # When creating a `MultipartEncoder` instance with data-types
74
+ # which don't have an `encode` method it will cause an error:
75
+ # object has no attribute 'encode'
76
+ # So convert common non-string types into strings.
75
77
if TYPE_CHECKING :
76
78
assert isinstance (post_data , dict )
77
79
for k , v in post_data .items ():
78
80
if isinstance (v , bool ):
79
- post_data [k ] = str (int (v ))
81
+ v = int (v )
82
+ if isinstance (v , (complex , float , int )):
83
+ post_data [k ] = str (v )
80
84
post_data ["file" ] = files .get ("file" )
81
85
post_data ["avatar" ] = files .get ("avatar" )
82
86
83
- data = MultipartEncoder (post_data )
87
+ data = MultipartEncoder (fields = post_data )
84
88
return SendData (data = data , content_type = data .content_type )
85
89
86
90
if raw and post_data :
Original file line number Diff line number Diff line change @@ -378,7 +378,7 @@ def import_group(
378
378
file : BinaryIO ,
379
379
path : str ,
380
380
name : str ,
381
- parent_id : Optional [str ] = None ,
381
+ parent_id : Optional [Union [ int , str ] ] = None ,
382
382
** kwargs : Any ,
383
383
) -> Union [Dict [str , Any ], requests .Response ]:
384
384
"""Import a group from an archive file.
@@ -399,7 +399,7 @@ def import_group(
399
399
A representation of the import status.
400
400
"""
401
401
files = {"file" : ("file.tar.gz" , file , "application/octet-stream" )}
402
- data = {"path" : path , "name" : name }
402
+ data : Dict [ str , Any ] = {"path" : path , "name" : name }
403
403
if parent_id is not None :
404
404
data ["parent_id" ] = parent_id
405
405
Original file line number Diff line number Diff line change @@ -25,14 +25,27 @@ def test_senddata_json_and_data(self) -> None:
25
25
26
26
27
27
class TestRequestsBackend :
28
- def test_prepare_send_data_str_parentid (self ) -> None :
29
- file = "12345"
30
- files = {"file" : ("file.tar.gz" , file , "application/octet-stream" )}
31
- post_data = {"parent_id" : "12" }
28
+ @pytest .mark .parametrize (
29
+ "test_data,expected" ,
30
+ [
31
+ (False , "0" ),
32
+ (True , "1" ),
33
+ ("12" , "12" ),
34
+ (12 , "12" ),
35
+ (12.0 , "12.0" ),
36
+ (complex (- 2 , 7 ), "(-2+7j)" ),
37
+ ],
38
+ )
39
+ def test_prepare_send_data_non_strings (self , test_data , expected ) -> None :
40
+ assert isinstance (expected , str )
41
+ files = {"file" : ("file.tar.gz" , "12345" , "application/octet-stream" )}
42
+ post_data = {"test_data" : test_data }
32
43
33
44
result = requests_backend .RequestsBackend .prepare_send_data (
34
45
files = files , post_data = post_data , raw = False
35
46
)
36
47
assert result .json is None
37
48
assert result .content_type .startswith ("multipart/form-data" )
38
49
assert isinstance (result .data , MultipartEncoder )
50
+ assert isinstance (result .data .fields ["test_data" ], str )
51
+ assert result .data .fields ["test_data" ] == expected
You can’t perform that action at this time.
0 commit comments