Skip to content

Commit 2cd15ac

Browse files
authored
Merge pull request #1696 from python-gitlab/jlvillal/mypy_merge_request_approvals
chore: add type-hints to gitlab/v4/objects/merge_request_approvals.py
2 parents 500895a + cf3a99a commit 2cd15ac

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

gitlab/v4/objects/merge_request_approvals.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, Dict, List, Optional, TYPE_CHECKING
2+
13
from gitlab import exceptions as exc
24
from gitlab.base import RequiredOptional, RESTManager, RESTObject
35
from gitlab.mixins import (
@@ -44,7 +46,12 @@ class ProjectApprovalManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
4446
_update_uses_post = True
4547

4648
@exc.on_http_error(exc.GitlabUpdateError)
47-
def set_approvers(self, approver_ids=None, approver_group_ids=None, **kwargs):
49+
def set_approvers(
50+
self,
51+
approver_ids: Optional[List[int]] = None,
52+
approver_group_ids: Optional[List[int]] = None,
53+
**kwargs: Any,
54+
) -> Dict[str, Any]:
4855
"""Change project-level allowed approvers and approver groups.
4956
5057
Args:
@@ -54,13 +61,21 @@ def set_approvers(self, approver_ids=None, approver_group_ids=None, **kwargs):
5461
Raises:
5562
GitlabAuthenticationError: If authentication is not correct
5663
GitlabUpdateError: If the server failed to perform the request
64+
65+
Returns:
66+
A dict value of the result
5767
"""
5868
approver_ids = approver_ids or []
5969
approver_group_ids = approver_group_ids or []
6070

71+
if TYPE_CHECKING:
72+
assert self._parent is not None
6173
path = f"/projects/{self._parent.get_id()}/approvers"
6274
data = {"approver_ids": approver_ids, "approver_group_ids": approver_group_ids}
63-
self.gitlab.http_put(path, post_data=data, **kwargs)
75+
result = self.gitlab.http_put(path, post_data=data, **kwargs)
76+
if TYPE_CHECKING:
77+
assert isinstance(result, dict)
78+
return result
6479

6580

6681
class ProjectApprovalRule(SaveMixin, ObjectDeleteMixin, RESTObject):
@@ -93,12 +108,12 @@ class ProjectMergeRequestApprovalManager(GetWithoutIdMixin, UpdateMixin, RESTMan
93108
@exc.on_http_error(exc.GitlabUpdateError)
94109
def set_approvers(
95110
self,
96-
approvals_required,
97-
approver_ids=None,
98-
approver_group_ids=None,
99-
approval_rule_name="name",
100-
**kwargs,
101-
):
111+
approvals_required: int,
112+
approver_ids: Optional[List[int]] = None,
113+
approver_group_ids: Optional[List[int]] = None,
114+
approval_rule_name: str = "name",
115+
**kwargs: Any,
116+
) -> RESTObject:
102117
"""Change MR-level allowed approvers and approver groups.
103118
104119
Args:
@@ -120,7 +135,11 @@ def set_approvers(
120135
"user_ids": approver_ids,
121136
"group_ids": approver_group_ids,
122137
}
123-
approval_rules = self._parent.approval_rules
138+
if TYPE_CHECKING:
139+
assert self._parent is not None
140+
approval_rules: ProjectMergeRequestApprovalRuleManager = (
141+
self._parent.approval_rules
142+
)
124143
""" update any existing approval rule matching the name"""
125144
existing_approval_rules = approval_rules.list()
126145
for ar in existing_approval_rules:
@@ -137,9 +156,10 @@ def set_approvers(
137156
class ProjectMergeRequestApprovalRule(SaveMixin, RESTObject):
138157
_id_attr = "approval_rule_id"
139158
_short_print_attr = "approval_rule"
159+
id: int
140160

141161
@exc.on_http_error(exc.GitlabUpdateError)
142-
def save(self, **kwargs):
162+
def save(self, **kwargs: Any) -> None:
143163
"""Save the changes made to the object to the server.
144164
145165
The object is updated to match what the server returns.
@@ -185,7 +205,9 @@ class ProjectMergeRequestApprovalRuleManager(
185205
optional=("approval_project_rule_id", "user_ids", "group_ids"),
186206
)
187207

188-
def create(self, data, **kwargs):
208+
def create(
209+
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
210+
) -> RESTObject:
189211
"""Create a new object.
190212
191213
Args:
@@ -202,6 +224,8 @@ def create(self, data, **kwargs):
202224
RESTObject: A new instance of the manage object class build with
203225
the data sent by the server
204226
"""
227+
if TYPE_CHECKING:
228+
assert data is not None
205229
new_data = data.copy()
206230
new_data["id"] = self._from_parent_attrs["project_id"]
207231
new_data["merge_request_iid"] = self._from_parent_attrs["mr_iid"]

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module = [
1818
"gitlab.v4.objects.issues",
1919
"gitlab.v4.objects.jobs",
2020
"gitlab.v4.objects.labels",
21-
"gitlab.v4.objects.merge_request_approvals",
2221
"gitlab.v4.objects.milestones",
2322
"gitlab.v4.objects.pipelines",
2423
"gitlab.v4.objects.repositories",

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