Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit90f96ac

Browse files
JohnVillalovosnejch
authored andcommitted
fix: support int forparent_id inimport_group
This will also fix other use cases where an integer is passed in toMultipartEncoder.Added unit tests to show it works.Closes:#2506
1 parentf2b5e4f commit90f96ac

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

‎gitlab/_backends/requests_backend.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,21 @@ def prepare_send_data(
7070
ifpost_dataisNone:
7171
post_data= {}
7272
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.
7577
ifTYPE_CHECKING:
7678
assertisinstance(post_data,dict)
7779
fork,vinpost_data.items():
7880
ifisinstance(v,bool):
79-
post_data[k]=str(int(v))
81+
v=int(v)
82+
ifisinstance(v, (complex,float,int)):
83+
post_data[k]=str(v)
8084
post_data["file"]=files.get("file")
8185
post_data["avatar"]=files.get("avatar")
8286

83-
data=MultipartEncoder(post_data)
87+
data=MultipartEncoder(fields=post_data)
8488
returnSendData(data=data,content_type=data.content_type)
8589

8690
ifrawandpost_data:

‎gitlab/v4/objects/groups.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def import_group(
378378
file:BinaryIO,
379379
path:str,
380380
name:str,
381-
parent_id:Optional[str]=None,
381+
parent_id:Optional[Union[int,str]]=None,
382382
**kwargs:Any,
383383
)->Union[Dict[str,Any],requests.Response]:
384384
"""Import a group from an archive file.
@@ -399,7 +399,7 @@ def import_group(
399399
A representation of the import status.
400400
"""
401401
files= {"file": ("file.tar.gz",file,"application/octet-stream")}
402-
data= {"path":path,"name":name}
402+
data:Dict[str,Any]= {"path":path,"name":name}
403403
ifparent_idisnotNone:
404404
data["parent_id"]=parent_id
405405

‎tests/unit/_backends/test_requests_backend.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,27 @@ def test_senddata_json_and_data(self) -> None:
2525

2626

2727
classTestRequestsBackend:
28-
deftest_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+
deftest_prepare_send_data_non_strings(self,test_data,expected)->None:
40+
assertisinstance(expected,str)
41+
files= {"file": ("file.tar.gz","12345","application/octet-stream")}
42+
post_data= {"test_data":test_data}
3243

3344
result=requests_backend.RequestsBackend.prepare_send_data(
3445
files=files,post_data=post_data,raw=False
3546
)
3647
assertresult.jsonisNone
3748
assertresult.content_type.startswith("multipart/form-data")
3849
assertisinstance(result.data,MultipartEncoder)
50+
assertisinstance(result.data.fields["test_data"],str)
51+
assertresult.data.fields["test_data"]==expected

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp