Skip to content

Commit

Permalink
Merge pull request #1678 from python-gitlab/jlvillal/mypy_commits
Browse files Browse the repository at this point in the history
chore: add type hints for gitlab/v4/objects/commits.py
  • Loading branch information
nejch committed Nov 8, 2021
2 parents 0e6fb5e + dc096a2 commit 9a2f54c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
33 changes: 24 additions & 9 deletions gitlab/v4/objects/commits.py
@@ -1,3 +1,7 @@
from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union

import requests

from gitlab import cli
from gitlab import exceptions as exc
from gitlab.base import RequiredOptional, RESTManager, RESTObject
Expand All @@ -24,7 +28,7 @@ class ProjectCommit(RESTObject):

@cli.register_custom_action("ProjectCommit")
@exc.on_http_error(exc.GitlabGetError)
def diff(self, **kwargs):
def diff(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Generate the commit diff.
Args:
Expand All @@ -42,7 +46,7 @@ def diff(self, **kwargs):

@cli.register_custom_action("ProjectCommit", ("branch",))
@exc.on_http_error(exc.GitlabCherryPickError)
def cherry_pick(self, branch, **kwargs):
def cherry_pick(self, branch: str, **kwargs: Any) -> None:
"""Cherry-pick a commit into a branch.
Args:
Expand All @@ -59,7 +63,9 @@ def cherry_pick(self, branch, **kwargs):

@cli.register_custom_action("ProjectCommit", optional=("type",))
@exc.on_http_error(exc.GitlabGetError)
def refs(self, type="all", **kwargs):
def refs(
self, type: str = "all", **kwargs: Any
) -> Union[Dict[str, Any], requests.Response]:
"""List the references the commit is pushed to.
Args:
Expand All @@ -79,7 +85,7 @@ def refs(self, type="all", **kwargs):

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

@cli.register_custom_action("ProjectCommit", ("branch",))
@exc.on_http_error(exc.GitlabRevertError)
def revert(self, branch, **kwargs):
def revert(
self, branch: str, **kwargs: Any
) -> Union[Dict[str, Any], requests.Response]:
"""Revert a commit on a given branch.
Args:
Expand All @@ -117,7 +125,7 @@ def revert(self, branch, **kwargs):

@cli.register_custom_action("ProjectCommit")
@exc.on_http_error(exc.GitlabGetError)
def signature(self, **kwargs):
def signature(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Get the signature of the commit.
Args:
Expand Down Expand Up @@ -172,7 +180,9 @@ class ProjectCommitStatusManager(ListMixin, CreateMixin, RESTManager):
)

@exc.on_http_error(exc.GitlabCreateError)
def create(self, data, **kwargs):
def create(
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
) -> ProjectCommitStatus:
"""Create a new object.
Args:
Expand All @@ -193,8 +203,13 @@ def create(self, data, **kwargs):
# they are missing when using only the API
# See #511
base_path = "/projects/%(project_id)s/statuses/%(commit_id)s"
if "project_id" in data and "commit_id" in data:
path: Optional[str]
if data is not None and "project_id" in data and "commit_id" in data:
path = base_path % data
else:
path = self._compute_path(base_path)
return CreateMixin.create(self, data, path=path, **kwargs)
if TYPE_CHECKING:
assert path is not None
return cast(
ProjectCommitStatus, CreateMixin.create(self, data, path=path, **kwargs)
)
1 change: 1 addition & 0 deletions pyproject.toml
Expand Up @@ -27,6 +27,7 @@ module = [
"gitlab.v4.objects.applications",
"gitlab.v4.objects.broadcast_messages",
"gitlab.v4.objects.deployments",
"gitlab.v4.objects.commits",
"gitlab.v4.objects.groups",
"gitlab.v4.objects.keys",
"gitlab.v4.objects.merge_requests",
Expand Down

0 comments on commit 9a2f54c

Please sign in to comment.