Skip to content

Commit 688a3ea

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 f8fad69 commit 688a3ea

File tree

12 files changed

+64
-80
lines changed

12 files changed

+64
-80
lines changed

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.
@@ -411,7 +413,7 @@ def lint(self, content: str, **kwargs: Any) -> Tuple[bool, List[str]]:
411413
post_data = {"content": content}
412414
data = self.http_post("/ci/lint", post_data=post_data, **kwargs)
413415
if TYPE_CHECKING:
414-
assert not isinstance(data, requests.Response)
416+
assert isinstance(data, dict)
415417
return (data["status"] == "valid", data["errors"])
416418

417419
@gitlab.exceptions.on_http_error(gitlab.exceptions.GitlabMarkdownError)
@@ -438,7 +440,7 @@ def markdown(
438440
post_data["project"] = project
439441
data = self.http_post("/markdown", post_data=post_data, **kwargs)
440442
if TYPE_CHECKING:
441-
assert not isinstance(data, requests.Response)
443+
assert isinstance(data, dict)
442444
assert isinstance(data["html"], str)
443445
return data["html"]
444446

@@ -479,7 +481,7 @@ def set_license(self, license: str, **kwargs: Any) -> Dict[str, Any]:
479481
data = {"license": license}
480482
result = self.http_post("/license", post_data=data, **kwargs)
481483
if TYPE_CHECKING:
482-
assert not isinstance(result, requests.Response)
484+
assert isinstance(result, dict)
483485
return result
484486

485487
def _set_auth_info(self) -> None:
@@ -778,7 +780,7 @@ def http_get(
778780
streamed: bool = False,
779781
raw: bool = False,
780782
**kwargs: Any,
781-
) -> Union[Dict[str, Any], requests.Response]:
783+
) -> HttpResponseType:
782784
"""Make a GET request to the Gitlab server.
783785
784786
Args:
@@ -958,7 +960,7 @@ def http_post(
958960
raw: bool = False,
959961
files: Optional[Dict[str, Any]] = None,
960962
**kwargs: Any,
961-
) -> Union[Dict[str, Any], requests.Response]:
963+
) -> HttpResponseType:
962964
"""Make a POST request to the Gitlab server.
963965
964966
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

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

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

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

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

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

836837

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

870871

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

898899

@@ -907,7 +908,7 @@ class PromoteMixin(_RestObjectBase):
907908

908909
def _get_update_method(
909910
self,
910-
) -> Callable[..., Union[Dict[str, Any], requests.Response]]:
911+
) -> Callable[..., gl_client.HttpResponseType]:
911912
"""Return the HTTP method to use.
912913
913914
Returns:
@@ -939,5 +940,5 @@ def promote(self, **kwargs: Any) -> Dict[str, Any]:
939940
http_method = self._get_update_method()
940941
result = http_method(path, **kwargs)
941942
if TYPE_CHECKING:
942-
assert not isinstance(result, requests.Response)
943+
assert isinstance(result, dict)
943944
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy