Skip to content

Commit 9a2f54c

Browse files
authored
Merge pull request #1678 from python-gitlab/jlvillal/mypy_commits
chore: add type hints for gitlab/v4/objects/commits.py
2 parents 0e6fb5e + dc096a2 commit 9a2f54c

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

gitlab/v4/objects/commits.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union
2+
3+
import requests
4+
15
from gitlab import cli
26
from gitlab import exceptions as exc
37
from gitlab.base import RequiredOptional, RESTManager, RESTObject
@@ -24,7 +28,7 @@ class ProjectCommit(RESTObject):
2428

2529
@cli.register_custom_action("ProjectCommit")
2630
@exc.on_http_error(exc.GitlabGetError)
27-
def diff(self, **kwargs):
31+
def diff(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
2832
"""Generate the commit diff.
2933
3034
Args:
@@ -42,7 +46,7 @@ def diff(self, **kwargs):
4246

4347
@cli.register_custom_action("ProjectCommit", ("branch",))
4448
@exc.on_http_error(exc.GitlabCherryPickError)
45-
def cherry_pick(self, branch, **kwargs):
49+
def cherry_pick(self, branch: str, **kwargs: Any) -> None:
4650
"""Cherry-pick a commit into a branch.
4751
4852
Args:
@@ -59,7 +63,9 @@ def cherry_pick(self, branch, **kwargs):
5963

6064
@cli.register_custom_action("ProjectCommit", optional=("type",))
6165
@exc.on_http_error(exc.GitlabGetError)
62-
def refs(self, type="all", **kwargs):
66+
def refs(
67+
self, type: str = "all", **kwargs: Any
68+
) -> Union[Dict[str, Any], requests.Response]:
6369
"""List the references the commit is pushed to.
6470
6571
Args:
@@ -79,7 +85,7 @@ def refs(self, type="all", **kwargs):
7985

8086
@cli.register_custom_action("ProjectCommit")
8187
@exc.on_http_error(exc.GitlabGetError)
82-
def merge_requests(self, **kwargs):
88+
def merge_requests(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
8389
"""List the merge requests related to the commit.
8490
8591
Args:
@@ -97,7 +103,9 @@ def merge_requests(self, **kwargs):
97103

98104
@cli.register_custom_action("ProjectCommit", ("branch",))
99105
@exc.on_http_error(exc.GitlabRevertError)
100-
def revert(self, branch, **kwargs):
106+
def revert(
107+
self, branch: str, **kwargs: Any
108+
) -> Union[Dict[str, Any], requests.Response]:
101109
"""Revert a commit on a given branch.
102110
103111
Args:
@@ -117,7 +125,7 @@ def revert(self, branch, **kwargs):
117125

118126
@cli.register_custom_action("ProjectCommit")
119127
@exc.on_http_error(exc.GitlabGetError)
120-
def signature(self, **kwargs):
128+
def signature(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
121129
"""Get the signature of the commit.
122130
123131
Args:
@@ -172,7 +180,9 @@ class ProjectCommitStatusManager(ListMixin, CreateMixin, RESTManager):
172180
)
173181

174182
@exc.on_http_error(exc.GitlabCreateError)
175-
def create(self, data, **kwargs):
183+
def create(
184+
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
185+
) -> ProjectCommitStatus:
176186
"""Create a new object.
177187
178188
Args:
@@ -193,8 +203,13 @@ def create(self, data, **kwargs):
193203
# they are missing when using only the API
194204
# See #511
195205
base_path = "/projects/%(project_id)s/statuses/%(commit_id)s"
196-
if "project_id" in data and "commit_id" in data:
206+
path: Optional[str]
207+
if data is not None and "project_id" in data and "commit_id" in data:
197208
path = base_path % data
198209
else:
199210
path = self._compute_path(base_path)
200-
return CreateMixin.create(self, data, path=path, **kwargs)
211+
if TYPE_CHECKING:
212+
assert path is not None
213+
return cast(
214+
ProjectCommitStatus, CreateMixin.create(self, data, path=path, **kwargs)
215+
)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module = [
2727
"gitlab.v4.objects.applications",
2828
"gitlab.v4.objects.broadcast_messages",
2929
"gitlab.v4.objects.deployments",
30+
"gitlab.v4.objects.commits",
3031
"gitlab.v4.objects.groups",
3132
"gitlab.v4.objects.keys",
3233
"gitlab.v4.objects.merge_requests",

0 commit comments

Comments
 (0)