From 93e39a2947c442fb91f5c80b34008ca1d27cdf71 Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Sun, 21 Nov 2021 14:24:49 -0800 Subject: [PATCH] chore: add type-hints to gitlab/v4/objects/issues.py --- gitlab/v4/objects/issues.py | 39 +++++++++++++++++++++++++++++++------ pyproject.toml | 1 - 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/gitlab/v4/objects/issues.py b/gitlab/v4/objects/issues.py index 5c397349b..8cd231768 100644 --- a/gitlab/v4/objects/issues.py +++ b/gitlab/v4/objects/issues.py @@ -1,3 +1,5 @@ +from typing import Any, cast, Dict, Tuple, TYPE_CHECKING, Union + from gitlab import cli from gitlab import exceptions as exc from gitlab import types @@ -65,6 +67,9 @@ class IssueManager(RetrieveMixin, RESTManager): ) _types = {"iids": types.ListAttribute, "labels": types.ListAttribute} + def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Issue: + return cast(Issue, super().get(id=id, lazy=lazy, **kwargs)) + class GroupIssue(RESTObject): pass @@ -116,7 +121,7 @@ class ProjectIssue( @cli.register_custom_action("ProjectIssue", ("to_project_id",)) @exc.on_http_error(exc.GitlabUpdateError) - def move(self, to_project_id, **kwargs): + def move(self, to_project_id: int, **kwargs: Any) -> None: """Move the issue to another project. Args: @@ -130,11 +135,13 @@ def move(self, to_project_id, **kwargs): path = f"{self.manager.path}/{self.get_id()}/move" data = {"to_project_id": to_project_id} server_data = self.manager.gitlab.http_post(path, post_data=data, **kwargs) + if TYPE_CHECKING: + assert isinstance(server_data, dict) self._update_attrs(server_data) @cli.register_custom_action("ProjectIssue") @exc.on_http_error(exc.GitlabGetError) - def related_merge_requests(self, **kwargs): + def related_merge_requests(self, **kwargs: Any) -> Dict[str, Any]: """List merge requests related to the issue. Args: @@ -148,11 +155,14 @@ def related_merge_requests(self, **kwargs): list: The list of merge requests. """ path = f"{self.manager.path}/{self.get_id()}/related_merge_requests" - return self.manager.gitlab.http_get(path, **kwargs) + result = self.manager.gitlab.http_get(path, **kwargs) + if TYPE_CHECKING: + assert isinstance(result, dict) + return result @cli.register_custom_action("ProjectIssue") @exc.on_http_error(exc.GitlabGetError) - def closed_by(self, **kwargs): + def closed_by(self, **kwargs: Any) -> Dict[str, Any]: """List merge requests that will close the issue when merged. Args: @@ -166,7 +176,10 @@ def closed_by(self, **kwargs): list: The list of merge requests. """ path = f"{self.manager.path}/{self.get_id()}/closed_by" - return self.manager.gitlab.http_get(path, **kwargs) + result = self.manager.gitlab.http_get(path, **kwargs) + if TYPE_CHECKING: + assert isinstance(result, dict) + return result class ProjectIssueManager(CRUDMixin, RESTManager): @@ -222,6 +235,11 @@ class ProjectIssueManager(CRUDMixin, RESTManager): ) _types = {"iids": types.ListAttribute, "labels": types.ListAttribute} + def get( + self, id: Union[str, int], lazy: bool = False, **kwargs: Any + ) -> ProjectIssue: + return cast(ProjectIssue, super().get(id=id, lazy=lazy, **kwargs)) + class ProjectIssueLink(ObjectDeleteMixin, RESTObject): _id_attr = "issue_link_id" @@ -234,7 +252,11 @@ class ProjectIssueLinkManager(ListMixin, CreateMixin, DeleteMixin, RESTManager): _create_attrs = RequiredOptional(required=("target_project_id", "target_issue_iid")) @exc.on_http_error(exc.GitlabCreateError) - def create(self, data, **kwargs): + # NOTE(jlvillal): Signature doesn't match CreateMixin.create() so ignore + # type error + def create( # type: ignore + self, data: Dict[str, Any], **kwargs: Any + ) -> Tuple[RESTObject, RESTObject]: """Create a new object. Args: @@ -250,7 +272,12 @@ def create(self, data, **kwargs): GitlabCreateError: If the server cannot perform the request """ self._check_missing_create_attrs(data) + if TYPE_CHECKING: + assert self.path is not None server_data = self.gitlab.http_post(self.path, post_data=data, **kwargs) + if TYPE_CHECKING: + assert isinstance(server_data, dict) + assert self._parent is not None source_issue = ProjectIssue(self._parent.manager, server_data["source_issue"]) target_issue = ProjectIssue(self._parent.manager, server_data["target_issue"]) return source_issue, target_issue diff --git a/pyproject.toml b/pyproject.toml index e19fab714..965d7ff3a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,6 @@ module = [ "docs.*", "docs.ext.*", "gitlab.v4.objects.files", - "gitlab.v4.objects.issues", "gitlab.v4.objects.jobs", "gitlab.v4.objects.labels", "gitlab.v4.objects.milestones",