Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8a0f9de

Browse files
chore: add type-hints to gitlab/v4/objects/merge_requests.py
* Add type-hints to gitlab/v4/objects/merge_requests.py * Add return value to cancel_merge_when_pipeline_succeeds() function as GitLab docs show it returns a value. * Add return value to approve() function as GitLab docs show it returns a value. * Add 'get()' method so that type-checkers will understand that getting a project merge request is of type ProjectMergeRequest.
1 parent4ab9e92 commit8a0f9de

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

‎gitlab/v4/objects/merge_requests.py‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
https://docs.gitlab.com/ee/api/merge_requests.html
44
https://docs.gitlab.com/ee/api/merge_request_approvals.html
55
"""
6+
fromtypingimportAny,cast,Dict,Optional,TYPE_CHECKING,Union
7+
8+
importrequests
9+
10+
importgitlab
611
fromgitlabimportcli
712
fromgitlabimportexceptionsasexc
813
fromgitlabimporttypes
@@ -159,7 +164,7 @@ class ProjectMergeRequest(
159164

160165
@cli.register_custom_action("ProjectMergeRequest")
161166
@exc.on_http_error(exc.GitlabMROnBuildSuccessError)
162-
defcancel_merge_when_pipeline_succeeds(self,**kwargs):
167+
defcancel_merge_when_pipeline_succeeds(self,**kwargs:Any)->ProjectMergeRequest:
163168
"""Cancel merge when the pipeline succeeds.
164169
165170
Args:
@@ -169,17 +174,23 @@ def cancel_merge_when_pipeline_succeeds(self, **kwargs):
169174
GitlabAuthenticationError: If authentication is not correct
170175
GitlabMROnBuildSuccessError: If the server could not handle the
171176
request
177+
178+
Returns:
179+
ProjectMergeRequest
172180
"""
173181

174182
path= (
175183
f"{self.manager.path}/{self.get_id()}/cancel_merge_when_pipeline_succeeds"
176184
)
177185
server_data=self.manager.gitlab.http_put(path,**kwargs)
186+
ifTYPE_CHECKING:
187+
assertisinstance(server_data,dict)
178188
self._update_attrs(server_data)
189+
returnProjectMergeRequest(self.manager,server_data)
179190

180191
@cli.register_custom_action("ProjectMergeRequest")
181192
@exc.on_http_error(exc.GitlabListError)
182-
defcloses_issues(self,**kwargs):
193+
defcloses_issues(self,**kwargs:Any)->RESTObjectList:
183194
"""List issues that will close on merge."
184195
185196
Args:
@@ -199,12 +210,14 @@ def closes_issues(self, **kwargs):
199210
"""
200211
path=f"{self.manager.path}/{self.get_id()}/closes_issues"
201212
data_list=self.manager.gitlab.http_list(path,as_list=False,**kwargs)
213+
ifTYPE_CHECKING:
214+
assertisinstance(data_list,gitlab.GitlabList)
202215
manager=ProjectIssueManager(self.manager.gitlab,parent=self.manager._parent)
203216
returnRESTObjectList(manager,ProjectIssue,data_list)
204217

205218
@cli.register_custom_action("ProjectMergeRequest")
206219
@exc.on_http_error(exc.GitlabListError)
207-
defcommits(self,**kwargs):
220+
defcommits(self,**kwargs:Any)->RESTObjectList:
208221
"""List the merge request commits.
209222
210223
Args:
@@ -225,12 +238,14 @@ def commits(self, **kwargs):
225238

226239
path=f"{self.manager.path}/{self.get_id()}/commits"
227240
data_list=self.manager.gitlab.http_list(path,as_list=False,**kwargs)
241+
ifTYPE_CHECKING:
242+
assertisinstance(data_list,gitlab.GitlabList)
228243
manager=ProjectCommitManager(self.manager.gitlab,parent=self.manager._parent)
229244
returnRESTObjectList(manager,ProjectCommit,data_list)
230245

231246
@cli.register_custom_action("ProjectMergeRequest")
232247
@exc.on_http_error(exc.GitlabListError)
233-
defchanges(self,**kwargs):
248+
defchanges(self,**kwargs:Any)->Union[Dict[str,Any],requests.Response]:
234249
"""List the merge request changes.
235250
236251
Args:
@@ -248,7 +263,7 @@ def changes(self, **kwargs):
248263

249264
@cli.register_custom_action("ProjectMergeRequest",tuple(), ("sha",))
250265
@exc.on_http_error(exc.GitlabMRApprovalError)
251-
defapprove(self,sha=None,**kwargs):
266+
defapprove(self,sha:Optional[str]=None,**kwargs:Any)->Dict[str,Any]:
252267
"""Approve the merge request.
253268
254269
Args:
@@ -259,6 +274,9 @@ def approve(self, sha=None, **kwargs):
259274
GitlabAuthenticationError: If authentication is not correct
260275
GitlabMRApprovalError: If the approval failed
261276
277+
Returns:
278+
A dict containing the result.
279+
262280
https://docs.gitlab.com/ee/api/merge_request_approvals.html#approve-merge-request
263281
"""
264282
path=f"{self.manager.path}/{self.get_id()}/approve"
@@ -267,11 +285,14 @@ def approve(self, sha=None, **kwargs):
267285
data["sha"]=sha
268286

269287
server_data=self.manager.gitlab.http_post(path,post_data=data,**kwargs)
288+
ifTYPE_CHECKING:
289+
assertisinstance(server_data,dict)
270290
self._update_attrs(server_data)
291+
returnserver_data
271292

272293
@cli.register_custom_action("ProjectMergeRequest")
273294
@exc.on_http_error(exc.GitlabMRApprovalError)
274-
defunapprove(self,**kwargs):
295+
defunapprove(self,**kwargs:Any)->None:
275296
"""Unapprove the merge request.
276297
277298
Args:
@@ -284,14 +305,16 @@ def unapprove(self, **kwargs):
284305
https://docs.gitlab.com/ee/api/merge_request_approvals.html#unapprove-merge-request
285306
"""
286307
path=f"{self.manager.path}/{self.get_id()}/unapprove"
287-
data= {}
308+
data:Dict[str,Any]= {}
288309

289310
server_data=self.manager.gitlab.http_post(path,post_data=data,**kwargs)
311+
ifTYPE_CHECKING:
312+
assertisinstance(server_data,dict)
290313
self._update_attrs(server_data)
291314

292315
@cli.register_custom_action("ProjectMergeRequest")
293316
@exc.on_http_error(exc.GitlabMRRebaseError)
294-
defrebase(self,**kwargs):
317+
defrebase(self,**kwargs:Any)->Union[Dict[str,Any],requests.Response]:
295318
"""Attempt to rebase the source branch onto the target branch
296319
297320
Args:
@@ -302,12 +325,12 @@ def rebase(self, **kwargs):
302325
GitlabMRRebaseError: If rebasing failed
303326
"""
304327
path=f"{self.manager.path}/{self.get_id()}/rebase"
305-
data= {}
328+
data:Dict[str,Any]= {}
306329
returnself.manager.gitlab.http_put(path,post_data=data,**kwargs)
307330

308331
@cli.register_custom_action("ProjectMergeRequest")
309332
@exc.on_http_error(exc.GitlabGetError)
310-
defmerge_ref(self,**kwargs):
333+
defmerge_ref(self,**kwargs:Any)->Union[Dict[str,Any],requests.Response]:
311334
"""Attempt to merge changes between source and target branches into
312335
`refs/merge-requests/:iid/merge`.
313336
@@ -332,15 +355,15 @@ def merge_ref(self, **kwargs):
332355
@exc.on_http_error(exc.GitlabMRClosedError)
333356
defmerge(
334357
self,
335-
merge_commit_message=None,
336-
should_remove_source_branch=False,
337-
merge_when_pipeline_succeeds=False,
338-
**kwargs,
339-
):
358+
merge_commit_message:Optional[str]=None,
359+
should_remove_source_branch:bool=False,
360+
merge_when_pipeline_succeeds:bool=False,
361+
**kwargs:Any,
362+
)->Dict[str,Any]:
340363
"""Accept the merge request.
341364
342365
Args:
343-
merge_commit_message (bool): Commit message
366+
merge_commit_message (str): Commit message
344367
should_remove_source_branch (bool): If True, removes the source
345368
branch
346369
merge_when_pipeline_succeeds (bool): Wait for the build to succeed,
@@ -352,7 +375,7 @@ def merge(
352375
GitlabMRClosedError: If the merge failed
353376
"""
354377
path=f"{self.manager.path}/{self.get_id()}/merge"
355-
data= {}
378+
data:Dict[str,Any]= {}
356379
ifmerge_commit_message:
357380
data["merge_commit_message"]=merge_commit_message
358381
ifshould_remove_source_branchisnotNone:
@@ -361,7 +384,10 @@ def merge(
361384
data["merge_when_pipeline_succeeds"]=True
362385

363386
server_data=self.manager.gitlab.http_put(path,post_data=data,**kwargs)
387+
ifTYPE_CHECKING:
388+
assertisinstance(server_data,dict)
364389
self._update_attrs(server_data)
390+
returnserver_data
365391

366392

367393
classProjectMergeRequestManager(CRUDMixin,RESTManager):
@@ -428,6 +454,11 @@ class ProjectMergeRequestManager(CRUDMixin, RESTManager):
428454
"labels":types.ListAttribute,
429455
}
430456

457+
defget(
458+
self,id:Union[str,int],lazy:bool=False,**kwargs:Any
459+
)->ProjectMergeRequest:
460+
returncast(ProjectMergeRequest,super().get(id=id,lazy=lazy,**kwargs))
461+
431462

432463
classProjectDeploymentMergeRequest(MergeRequest):
433464
pass

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ignore_errors = true
2323

2424
[[tool.mypy.overrides]]# Overrides to negate above patterns
2525
module = [
26+
"gitlab.v4.objects.merge_requests",
2627
"gitlab.v4.objects.projects",
2728
"gitlab.v4.objects.users"
2829
]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp