Skip to content

Commit 90f96ac

Browse files
JohnVillalovosnejch
authored andcommitted
fix: support int for parent_id in import_group
This will also fix other use cases where an integer is passed in to MultipartEncoder. Added unit tests to show it works. Closes: #2506
1 parent f2b5e4f commit 90f96ac

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

gitlab/_backends/requests_backend.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,21 @@ def prepare_send_data(
7070
if post_data is None:
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
if TYPE_CHECKING:
7678
assert isinstance(post_data, dict)
7779
for k, v in post_data.items():
7880
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)
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
return SendData(data=data, content_type=data.content_type)
8589

8690
if raw and post_data:

gitlab/v4/objects/groups.py

+2-2
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
if parent_id is not None:
404404
data["parent_id"] = parent_id
405405

tests/unit/_backends/test_requests_backend.py

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

2626

2727
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}
3243

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

0 commit comments

Comments
 (0)