Skip to content

chore: add type hints for gitlab/v4/objects/commits.py #1678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions gitlab/v4/objects/commits.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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