Skip to content

Commit 7555b3a

Browse files
chore: correct type-hints for http_put/http_post
http_put/http_post will either return a JSON object or nothing. We never expect them to return a raw requests.response object. Update type-hints and code to no longer have a requests.response object as a possible return value.
1 parent a349793 commit 7555b3a

19 files changed

+41
-113
lines changed

gitlab/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ def http_post(
831831
raw: bool = False,
832832
files: Optional[Dict[str, Any]] = None,
833833
**kwargs: Any,
834-
) -> Union[Dict[str, Any], requests.Response]:
834+
) -> Dict[str, Any]:
835835
"""Make a POST request to the Gitlab server.
836836
837837
Args:
@@ -870,7 +870,7 @@ def http_post(
870870
raise gitlab.exceptions.GitlabParsingError(
871871
error_message="Failed to parse the server message"
872872
) from e
873-
return result
873+
return {}
874874

875875
def http_put(
876876
self,
@@ -880,7 +880,7 @@ def http_put(
880880
raw: bool = False,
881881
files: Optional[Dict[str, Any]] = None,
882882
**kwargs: Any,
883-
) -> Union[Dict[str, Any], requests.Response]:
883+
) -> Dict[str, Any]:
884884
"""Make a PUT request to the Gitlab server.
885885
886886
Args:

gitlab/v4/objects/commits.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def diff(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
4646

4747
@cli.register_custom_action("ProjectCommit", ("branch",))
4848
@exc.on_http_error(exc.GitlabCherryPickError)
49-
def cherry_pick(self, branch: str, **kwargs: Any) -> None:
49+
def cherry_pick(self, branch: str, **kwargs: Any) -> Dict[str, Any]:
5050
"""Cherry-pick a commit into a branch.
5151
5252
Args:
@@ -59,7 +59,7 @@ def cherry_pick(self, branch: str, **kwargs: Any) -> None:
5959
"""
6060
path = f"{self.manager.path}/{self.get_id()}/cherry_pick"
6161
post_data = {"branch": branch}
62-
self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)
62+
return self.manager.gitlab.http_post(path, post_data=post_data, **kwargs)
6363

6464
@cli.register_custom_action("ProjectCommit", optional=("type",))
6565
@exc.on_http_error(exc.GitlabGetError)
@@ -103,9 +103,7 @@ def merge_requests(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Respon
103103

104104
@cli.register_custom_action("ProjectCommit", ("branch",))
105105
@exc.on_http_error(exc.GitlabRevertError)
106-
def revert(
107-
self, branch: str, **kwargs: Any
108-
) -> Union[Dict[str, Any], requests.Response]:
106+
def revert(self, branch: str, **kwargs: Any) -> Dict[str, Any]:
109107
"""Revert a commit on a given branch.
110108
111109
Args:

gitlab/v4/objects/deploy_keys.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from typing import Any, cast, Dict, Union
22

3-
import requests
4-
53
from gitlab import cli
64
from gitlab import exceptions as exc
75
from gitlab.base import RequiredOptional, RESTManager, RESTObject
@@ -37,9 +35,7 @@ class ProjectKeyManager(CRUDMixin, RESTManager):
3735

3836
@cli.register_custom_action("ProjectKeyManager", ("key_id",))
3937
@exc.on_http_error(exc.GitlabProjectDeployKeyError)
40-
def enable(
41-
self, key_id: int, **kwargs: Any
42-
) -> Union[Dict[str, Any], requests.Response]:
38+
def enable(self, key_id: int, **kwargs: Any) -> Dict[str, Any]:
4339
"""Enable a deploy key for a project.
4440
4541
Args:

gitlab/v4/objects/environments.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from typing import Any, cast, Dict, Union
22

3-
import requests
4-
53
from gitlab import cli
64
from gitlab import exceptions as exc
75
from gitlab.base import RequiredOptional, RESTManager, RESTObject
@@ -23,7 +21,7 @@
2321
class ProjectEnvironment(SaveMixin, ObjectDeleteMixin, RESTObject):
2422
@cli.register_custom_action("ProjectEnvironment")
2523
@exc.on_http_error(exc.GitlabStopError)
26-
def stop(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
24+
def stop(self, **kwargs: Any) -> Dict[str, Any]:
2725
"""Stop the environment.
2826
2927
Args:

gitlab/v4/objects/epics.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ def create(
109109
CreateMixin._check_missing_create_attrs(self, data)
110110
path = f"{self.path}/{data.pop('issue_id')}"
111111
server_data = self.gitlab.http_post(path, **kwargs)
112-
if TYPE_CHECKING:
113-
assert isinstance(server_data, dict)
114112
# The epic_issue_id attribute doesn't exist when creating the resource,
115113
# but is used everywhere elese. Let's create it to be consistent client
116114
# side

gitlab/v4/objects/features.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
GitLab API:
33
https://docs.gitlab.com/ee/api/features.html
44
"""
5-
from typing import Any, Optional, TYPE_CHECKING, Union
5+
from typing import Any, Optional, Union
66

77
from gitlab import exceptions as exc
88
from gitlab import utils
@@ -62,6 +62,4 @@ def set(
6262
}
6363
data = utils.remove_none_from_dict(data)
6464
server_data = self.gitlab.http_post(path, post_data=data, **kwargs)
65-
if TYPE_CHECKING:
66-
assert isinstance(server_data, dict)
6765
return self._obj_cls(self, server_data)

gitlab/v4/objects/files.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ def create(
147147
file_path = new_data.pop("file_path").replace("/", "%2F")
148148
path = f"{self.path}/{file_path}"
149149
server_data = self.gitlab.http_post(path, post_data=new_data, **kwargs)
150-
if TYPE_CHECKING:
151-
assert isinstance(server_data, dict)
152150
return self._obj_cls(self, server_data)
153151

154152
@exc.on_http_error(exc.GitlabUpdateError)
@@ -178,8 +176,6 @@ def update( # type: ignore
178176
path = f"{self.path}/{file_path}"
179177
self._check_missing_update_attrs(data)
180178
result = self.gitlab.http_put(path, post_data=data, **kwargs)
181-
if TYPE_CHECKING:
182-
assert isinstance(result, dict)
183179
return result
184180

185181
@cli.register_custom_action(

gitlab/v4/objects/geo_nodes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ def repair(self, **kwargs: Any) -> None:
3232
"""
3333
path = f"/geo_nodes/{self.get_id()}/repair"
3434
server_data = self.manager.gitlab.http_post(path, **kwargs)
35-
if TYPE_CHECKING:
36-
assert isinstance(server_data, dict)
3735
self._update_attrs(server_data)
3836

3937
@cli.register_custom_action("GeoNode")

gitlab/v4/objects/groups.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from typing import Any, BinaryIO, cast, Dict, List, Optional, Type, TYPE_CHECKING, Union
2-
3-
import requests
1+
from typing import Any, BinaryIO, cast, Dict, List, Optional, Type, Union
42

53
import gitlab
64
from gitlab import cli
@@ -205,8 +203,6 @@ def share(
205203
"expires_at": expires_at,
206204
}
207205
server_data = self.manager.gitlab.http_post(path, post_data=data, **kwargs)
208-
if TYPE_CHECKING:
209-
assert isinstance(server_data, dict)
210206
self._update_attrs(server_data)
211207

212208
@cli.register_custom_action("Group", ("group_id",))
@@ -303,7 +299,7 @@ def import_group(
303299
name: str,
304300
parent_id: Optional[str] = None,
305301
**kwargs: Any,
306-
) -> Union[Dict[str, Any], requests.Response]:
302+
) -> Dict[str, Any]:
307303
"""Import a group from an archive file.
308304
309305
Args:

gitlab/v4/objects/issues.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ def move(self, to_project_id: int, **kwargs: Any) -> None:
135135
path = f"{self.manager.path}/{self.get_id()}/move"
136136
data = {"to_project_id": to_project_id}
137137
server_data = self.manager.gitlab.http_post(path, post_data=data, **kwargs)
138-
if TYPE_CHECKING:
139-
assert isinstance(server_data, dict)
140138
self._update_attrs(server_data)
141139

142140
@cli.register_custom_action("ProjectIssue")
@@ -276,7 +274,6 @@ def create( # type: ignore
276274
assert self.path is not None
277275
server_data = self.gitlab.http_post(self.path, post_data=data, **kwargs)
278276
if TYPE_CHECKING:
279-
assert isinstance(server_data, dict)
280277
assert self._parent is not None
281278
source_issue = ProjectIssue(self._parent.manager, server_data["source_issue"])
282279
target_issue = ProjectIssue(self._parent.manager, server_data["target_issue"])

0 commit comments

Comments
 (0)