Skip to content

Commit

Permalink
Merge pull request #1691 from python-gitlab/jlvillal/mypy_snippets
Browse files Browse the repository at this point in the history
chore: add type-hints to gitlab/v4/objects/snippets.py
  • Loading branch information
nejch committed Nov 14, 2021
2 parents a544cd5 + f256d4f commit f775668
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
36 changes: 32 additions & 4 deletions gitlab/v4/objects/snippets.py
@@ -1,7 +1,11 @@
from typing import Any, Callable, cast, List, Optional, TYPE_CHECKING, Union

import requests

from gitlab import cli
from gitlab import exceptions as exc
from gitlab import utils
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject, RESTObjectList
from gitlab.mixins import CRUDMixin, ObjectDeleteMixin, SaveMixin, UserAgentDetailMixin

from .award_emojis import ProjectSnippetAwardEmojiManager # noqa: F401
Expand All @@ -21,7 +25,13 @@ class Snippet(UserAgentDetailMixin, SaveMixin, ObjectDeleteMixin, RESTObject):

@cli.register_custom_action("Snippet")
@exc.on_http_error(exc.GitlabGetError)
def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
def content(
self,
streamed: bool = False,
action: Optional[Callable[..., Any]] = None,
chunk_size: int = 1024,
**kwargs: Any,
) -> Optional[bytes]:
"""Return the content of a snippet.
Args:
Expand All @@ -44,6 +54,8 @@ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
result = self.manager.gitlab.http_get(
path, streamed=streamed, raw=True, **kwargs
)
if TYPE_CHECKING:
assert isinstance(result, requests.Response)
return utils.response_content(result, streamed, action, chunk_size)


Expand All @@ -58,7 +70,7 @@ class SnippetManager(CRUDMixin, RESTManager):
)

@cli.register_custom_action("SnippetManager")
def public(self, **kwargs):
def public(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
"""List all the public snippets.
Args:
Expand All @@ -73,6 +85,9 @@ def public(self, **kwargs):
"""
return self.list(path="/snippets/public", **kwargs)

def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Snippet:
return cast(Snippet, super().get(id=id, lazy=lazy, **kwargs))


class ProjectSnippet(UserAgentDetailMixin, SaveMixin, ObjectDeleteMixin, RESTObject):
_url = "/projects/{project_id}/snippets"
Expand All @@ -84,7 +99,13 @@ class ProjectSnippet(UserAgentDetailMixin, SaveMixin, ObjectDeleteMixin, RESTObj

@cli.register_custom_action("ProjectSnippet")
@exc.on_http_error(exc.GitlabGetError)
def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
def content(
self,
streamed: bool = False,
action: Optional[Callable[..., Any]] = None,
chunk_size: int = 1024,
**kwargs: Any,
) -> Optional[bytes]:
"""Return the content of a snippet.
Args:
Expand All @@ -107,6 +128,8 @@ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
result = self.manager.gitlab.http_get(
path, streamed=streamed, raw=True, **kwargs
)
if TYPE_CHECKING:
assert isinstance(result, requests.Response)
return utils.response_content(result, streamed, action, chunk_size)


Expand All @@ -121,3 +144,8 @@ class ProjectSnippetManager(CRUDMixin, RESTManager):
_update_attrs = RequiredOptional(
optional=("title", "file_name", "content", "visibility", "description"),
)

def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
) -> ProjectSnippet:
return cast(ProjectSnippet, super().get(id=id, lazy=lazy, **kwargs))
1 change: 0 additions & 1 deletion pyproject.toml
Expand Up @@ -24,7 +24,6 @@ module = [
"gitlab.v4.objects.repositories",
"gitlab.v4.objects.services",
"gitlab.v4.objects.sidekiq",
"gitlab.v4.objects.snippets",
"setup",
"tests.functional.*",
"tests.functional.api.*",
Expand Down

0 comments on commit f775668

Please sign in to comment.