Skip to content

Commit 1f9becb

Browse files
chore: correct type-hints for http_get/http_post derived values
The methods `http_get()` and `http_post()` can also return a `list`. Update the code to handle that case. Closes: #2158
1 parent fcbced8 commit 1f9becb

12 files changed

Lines changed: 64 additions & 80 deletions

File tree

‎gitlab/client.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
f"api-usage.html#pagination"
4545
)
4646

47+
HttpResponseType = Union[Dict[str, Any], List[Any], requests.Response]
48+
4749

4850
class Gitlab:
4951
"""Represents a GitLab server connection.
@@ -409,7 +411,7 @@ def lint(self, content: str, **kwargs: Any) -> Tuple[bool, List[str]]:
409411
post_data = {"content": content}
410412
data = self.http_post("/ci/lint", post_data=post_data, **kwargs)
411413
if TYPE_CHECKING:
412-
assert not isinstance(data, requests.Response)
414+
assert isinstance(data, dict)
413415
return (data["status"] == "valid", data["errors"])
414416

415417
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabMarkdownError)
@@ -436,7 +438,7 @@ def markdown(
436438
post_data["project"] = project
437439
data = self.http_post("/markdown", post_data=post_data, **kwargs)
438440
if TYPE_CHECKING:
439-
assert not isinstance(data, requests.Response)
441+
assert isinstance(data, dict)
440442
return data["html"]
441443

442444
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabLicenseError)
@@ -476,7 +478,7 @@ def set_license(self, license: str, **kwargs: Any) -> Dict[str, Any]:
476478
data = {"license": license}
477479
result = self.http_post("/license", post_data=data, **kwargs)
478480
if TYPE_CHECKING:
479-
assert not isinstance(result, requests.Response)
481+
assert isinstance(result, dict)
480482
return result
481483

482484
def _set_auth_info(self) -> None:
@@ -775,7 +777,7 @@ def http_get(
775777
streamed: bool = False,
776778
raw: bool = False,
777779
**kwargs: Any,
778-
) -> Union[Dict[str, Any], requests.Response]:
780+
) -> HttpResponseType:
779781
"""Make a GET request to the Gitlab server.
780782
781783
Args:
@@ -946,7 +948,7 @@ def http_post(
946948
raw: bool = False,
947949
files: Optional[Dict[str, Any]] = None,
948950
**kwargs: Any,
949-
) -> Union[Dict[str, Any], requests.Response]:
951+
) -> HttpResponseType:
950952
"""Make a POST request to the Gitlab server.
951953
952954
Args:

‎gitlab/mixins.py‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import requests
3333

3434
import gitlab
35+
import gitlab.client as gl_client
3536
from gitlab import base, cli
3637
from gitlab import exceptions as exc
3738
from gitlab import utils
@@ -138,7 +139,7 @@ def get(
138139
return self._obj_cls(self, {self._obj_cls._id_attr: id}, lazy=lazy)
139140
server_data = self.gitlab.http_get(path, **kwargs)
140141
if TYPE_CHECKING:
141-
assert not isinstance(server_data, requests.Response)
142+
assert isinstance(server_data, dict)
142143
return self._obj_cls(self, server_data, lazy=lazy)
143144

144145

@@ -170,7 +171,7 @@ def get(self, **kwargs: Any) -> base.RESTObject:
170171
assert self.path is not None
171172
server_data = self.gitlab.http_get(self.path, **kwargs)
172173
if TYPE_CHECKING:
173-
assert not isinstance(server_data, requests.Response)
174+
assert isinstance(server_data, dict)
174175
assert self._obj_cls is not None
175176
return self._obj_cls(self, server_data)
176177

@@ -204,7 +205,7 @@ def refresh(self, **kwargs: Any) -> None:
204205
path = self.manager.path
205206
server_data = self.manager.gitlab.http_get(path, **kwargs)
206207
if TYPE_CHECKING:
207-
assert not isinstance(server_data, requests.Response)
208+
assert isinstance(server_data, dict)
208209
self._update_attrs(server_data)
209210

210211

@@ -309,7 +310,7 @@ def create(
309310
path = kwargs.pop("path", self.path)
310311
server_data = self.gitlab.http_post(path, post_data=data, files=files, **kwargs)
311312
if TYPE_CHECKING:
312-
assert not isinstance(server_data, requests.Response)
313+
assert isinstance(server_data, dict)
313314
assert self._obj_cls is not None
314315
return self._obj_cls(self, server_data)
315316

@@ -326,7 +327,7 @@ class UpdateMixin(_RestManagerBase):
326327

327328
def _get_update_method(
328329
self,
329-
) -> Callable[..., Union[Dict[str, Any], requests.Response]]:
330+
) -> Callable[..., gl_client.HttpResponseType]:
330331
"""Return the HTTP method to use.
331332
332333
Returns:
@@ -375,7 +376,7 @@ def update(
375376
http_method = self._get_update_method()
376377
result = http_method(path, post_data=new_data, files=files, **kwargs)
377378
if TYPE_CHECKING:
378-
assert not isinstance(result, requests.Response)
379+
assert isinstance(result, dict)
379380
return result
380381

381382

@@ -562,7 +563,7 @@ def user_agent_detail(self, **kwargs: Any) -> Dict[str, Any]:
562563
path = f"{self.manager.path}/{self.encoded_id}/user_agent_detail"
563564
result = self.manager.gitlab.http_get(path, **kwargs)
564565
if TYPE_CHECKING:
565-
assert not isinstance(result, requests.Response)
566+
assert isinstance(result, dict)
566567
return result
567568

568569

@@ -675,7 +676,7 @@ def subscribe(self, **kwargs: Any) -> None:
675676
path = f"{self.manager.path}/{self.encoded_id}/subscribe"
676677
server_data = self.manager.gitlab.http_post(path, **kwargs)
677678
if TYPE_CHECKING:
678-
assert not isinstance(server_data, requests.Response)
679+
assert isinstance(server_data, dict)
679680
self._update_attrs(server_data)
680681

681682
@cli.register_custom_action(
@@ -695,7 +696,7 @@ def unsubscribe(self, **kwargs: Any) -> None:
695696
path = f"{self.manager.path}/{self.encoded_id}/unsubscribe"
696697
server_data = self.manager.gitlab.http_post(path, **kwargs)
697698
if TYPE_CHECKING:
698-
assert not isinstance(server_data, requests.Response)
699+
assert isinstance(server_data, dict)
699700
self._update_attrs(server_data)
700701

701702

@@ -751,7 +752,7 @@ def time_stats(self, **kwargs: Any) -> Dict[str, Any]:
751752
path = f"{self.manager.path}/{self.encoded_id}/time_stats"
752753
result = self.manager.gitlab.http_get(path, **kwargs)
753754
if TYPE_CHECKING:
754-
assert not isinstance(result, requests.Response)
755+
assert isinstance(result, dict)
755756
return result
756757

757758
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"), ("duration",))
@@ -771,7 +772,7 @@ def time_estimate(self, duration: str, **kwargs: Any) -> Dict[str, Any]:
771772
data = {"duration": duration}
772773
result = self.manager.gitlab.http_post(path, post_data=data, **kwargs)
773774
if TYPE_CHECKING:
774-
assert not isinstance(result, requests.Response)
775+
assert isinstance(result, dict)
775776
return result
776777

777778
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"))
@@ -789,7 +790,7 @@ def reset_time_estimate(self, **kwargs: Any) -> Dict[str, Any]:
789790
path = f"{self.manager.path}/{self.encoded_id}/reset_time_estimate"
790791
result = self.manager.gitlab.http_post(path, **kwargs)
791792
if TYPE_CHECKING:
792-
assert not isinstance(result, requests.Response)
793+
assert isinstance(result, dict)
793794
return result
794795

795796
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"), ("duration",))
@@ -809,7 +810,7 @@ def add_spent_time(self, duration: str, **kwargs: Any) -> Dict[str, Any]:
809810
data = {"duration": duration}
810811
result = self.manager.gitlab.http_post(path, post_data=data, **kwargs)
811812
if TYPE_CHECKING:
812-
assert not isinstance(result, requests.Response)
813+
assert isinstance(result, dict)
813814
return result
814815

815816
@cli.register_custom_action(("ProjectIssue", "ProjectMergeRequest"))
@@ -827,7 +828,7 @@ def reset_spent_time(self, **kwargs: Any) -> Dict[str, Any]:
827828
path = f"{self.manager.path}/{self.encoded_id}/reset_spent_time"
828829
result = self.manager.gitlab.http_post(path, **kwargs)
829830
if TYPE_CHECKING:
830-
assert not isinstance(result, requests.Response)
831+
assert isinstance(result, dict)
831832
return result
832833

833834

@@ -861,7 +862,7 @@ def participants(self, **kwargs: Any) -> Dict[str, Any]:
861862
path = f"{self.manager.path}/{self.encoded_id}/participants"
862863
result = self.manager.gitlab.http_get(path, **kwargs)
863864
if TYPE_CHECKING:
864-
assert not isinstance(result, requests.Response)
865+
assert isinstance(result, dict)
865866
return result
866867

867868

@@ -889,7 +890,7 @@ def render(self, link_url: str, image_url: str, **kwargs: Any) -> Dict[str, Any]
889890
data = {"link_url": link_url, "image_url": image_url}
890891
result = self.gitlab.http_get(path, data, **kwargs)
891892
if TYPE_CHECKING:
892-
assert not isinstance(result, requests.Response)
893+
assert isinstance(result, dict)
893894
return result
894895

895896

@@ -904,7 +905,7 @@ class PromoteMixin(_RestObjectBase):
904905

905906
def _get_update_method(
906907
self,
907-
) -> Callable[..., Union[Dict[str, Any], requests.Response]]:
908+
) -> Callable[..., gl_client.HttpResponseType]:
908909
"""Return the HTTP method to use.
909910
910911
Returns:
@@ -936,5 +937,5 @@ def promote(self, **kwargs: Any) -> Dict[str, Any]:
936937
http_method = self._get_update_method()
937938
result = http_method(path, **kwargs)
938939
if TYPE_CHECKING:
939-
assert not isinstance(result, requests.Response)
940+
assert isinstance(result, dict)
940941
return result

‎gitlab/v4/objects/commits.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from typing import Any, cast, Dict, List, Optional, TYPE_CHECKING, Union
22

3-
import requests
4-
53
import gitlab
64
from gitlab import cli
75
from gitlab import exceptions as exc
@@ -107,9 +105,7 @@ def merge_requests(
107105

108106
@cli.register_custom_action("ProjectCommit", ("branch",))
109107
@exc.on_http_error(exc.GitlabRevertError)
110-
def revert(
111-
self, branch: str, **kwargs: Any
112-
) -> Union[Dict[str, Any], requests.Response]:
108+
def revert(self, branch: str, **kwargs: Any) -> gitlab.client.HttpResponseType:
113109
"""Revert a commit on a given branch.
114110
115111
Args:
@@ -129,7 +125,7 @@ def revert(
129125

130126
@cli.register_custom_action("ProjectCommit")
131127
@exc.on_http_error(exc.GitlabGetError)
132-
def signature(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
128+
def signature(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
133129
"""Get the signature of the commit.
134130
135131
Args:

‎gitlab/v4/objects/deploy_keys.py‎

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

3+
import gitlab
54
from gitlab import cli
65
from gitlab import exceptions as exc
76
from gitlab.base import RESTManager, RESTObject
@@ -38,9 +37,7 @@ class ProjectKeyManager(CRUDMixin, RESTManager):
3837

3938
@cli.register_custom_action("ProjectKeyManager", ("key_id",))
4039
@exc.on_http_error(exc.GitlabProjectDeployKeyError)
41-
def enable(
42-
self, key_id: int, **kwargs: Any
43-
) -> Union[Dict[str, Any], requests.Response]:
40+
def enable(self, key_id: int, **kwargs: Any) -> gitlab.client.HttpResponseType:
4441
"""Enable a deploy key for a project.
4542
4643
Args:

‎gitlab/v4/objects/environments.py‎

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

3+
import gitlab
54
from gitlab import cli
65
from gitlab import exceptions as exc
76
from gitlab.base import RESTManager, RESTObject
@@ -26,7 +25,7 @@
2625
class ProjectEnvironment(SaveMixin, ObjectDeleteMixin, RESTObject):
2726
@cli.register_custom_action("ProjectEnvironment")
2827
@exc.on_http_error(exc.GitlabStopError)
29-
def stop(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
28+
def stop(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
3029
"""Stop the environment.
3130
3231
Args:

‎gitlab/v4/objects/groups.py‎

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

3-
import requests
4-
53
import gitlab
64
from gitlab import cli
75
from gitlab import exceptions as exc
@@ -332,7 +330,7 @@ def import_group(
332330
name: str,
333331
parent_id: Optional[str] = None,
334332
**kwargs: Any,
335-
) -> Union[Dict[str, Any], requests.Response]:
333+
) -> gitlab.client.HttpResponseType:
336334
"""Import a group from an archive file.
337335
338336
Args:

‎gitlab/v4/objects/merge_requests.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"""
66
from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union
77

8-
import requests
9-
108
import gitlab
119
from gitlab import cli
1210
from gitlab import exceptions as exc
@@ -244,7 +242,7 @@ def commits(self, **kwargs: Any) -> RESTObjectList:
244242

245243
@cli.register_custom_action("ProjectMergeRequest")
246244
@exc.on_http_error(exc.GitlabListError)
247-
def changes(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
245+
def changes(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
248246
"""List the merge request changes.
249247
250248
Args:
@@ -313,7 +311,7 @@ def unapprove(self, **kwargs: Any) -> None:
313311

314312
@cli.register_custom_action("ProjectMergeRequest")
315313
@exc.on_http_error(exc.GitlabMRRebaseError)
316-
def rebase(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
314+
def rebase(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
317315
"""Attempt to rebase the source branch onto the target branch
318316
319317
Args:
@@ -329,7 +327,7 @@ def rebase(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
329327

330328
@cli.register_custom_action("ProjectMergeRequest")
331329
@exc.on_http_error(exc.GitlabGetError)
332-
def merge_ref(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
330+
def merge_ref(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
333331
"""Attempt to merge changes between source and target branches into
334332
`refs/merge-requests/:iid/merge`.
335333

‎gitlab/v4/objects/pipelines.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union
22

3-
import requests
4-
3+
import gitlab
54
from gitlab import cli
65
from gitlab import exceptions as exc
76
from gitlab.base import RESTManager, RESTObject
@@ -60,7 +59,7 @@ class ProjectPipeline(RefreshMixin, ObjectDeleteMixin, RESTObject):
6059

6160
@cli.register_custom_action("ProjectPipeline")
6261
@exc.on_http_error(exc.GitlabPipelineCancelError)
63-
def cancel(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
62+
def cancel(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
6463
"""Cancel the job.
6564
6665
Args:
@@ -75,7 +74,7 @@ def cancel(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
7574

7675
@cli.register_custom_action("ProjectPipeline")
7776
@exc.on_http_error(exc.GitlabPipelineRetryError)
78-
def retry(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
77+
def retry(self, **kwargs: Any) -> gitlab.client.HttpResponseType:
7978
"""Retry the job.
8079
8180
Args:

0 commit comments

Comments
 (0)