Skip to content

chore: add type-hints to gitlab/v4/objects/snippets.py #1691

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 14, 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
36 changes: 32 additions & 4 deletions gitlab/v4/objects/snippets.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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