Skip to content

Commit f775668

Browse files
authored
Merge pull request #1691 from python-gitlab/jlvillal/mypy_snippets
chore: add type-hints to gitlab/v4/objects/snippets.py
2 parents a544cd5 + f256d4f commit f775668

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

gitlab/v4/objects/snippets.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from typing import Any, Callable, cast, List, Optional, TYPE_CHECKING, Union
2+
3+
import requests
4+
15
from gitlab import cli
26
from gitlab import exceptions as exc
37
from gitlab import utils
4-
from gitlab.base import RequiredOptional, RESTManager, RESTObject
8+
from gitlab.base import RequiredOptional, RESTManager, RESTObject, RESTObjectList
59
from gitlab.mixins import CRUDMixin, ObjectDeleteMixin, SaveMixin, UserAgentDetailMixin
610

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

2226
@cli.register_custom_action("Snippet")
2327
@exc.on_http_error(exc.GitlabGetError)
24-
def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
28+
def content(
29+
self,
30+
streamed: bool = False,
31+
action: Optional[Callable[..., Any]] = None,
32+
chunk_size: int = 1024,
33+
**kwargs: Any,
34+
) -> Optional[bytes]:
2535
"""Return the content of a snippet.
2636
2737
Args:
@@ -44,6 +54,8 @@ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
4454
result = self.manager.gitlab.http_get(
4555
path, streamed=streamed, raw=True, **kwargs
4656
)
57+
if TYPE_CHECKING:
58+
assert isinstance(result, requests.Response)
4759
return utils.response_content(result, streamed, action, chunk_size)
4860

4961

@@ -58,7 +70,7 @@ class SnippetManager(CRUDMixin, RESTManager):
5870
)
5971

6072
@cli.register_custom_action("SnippetManager")
61-
def public(self, **kwargs):
73+
def public(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
6274
"""List all the public snippets.
6375
6476
Args:
@@ -73,6 +85,9 @@ def public(self, **kwargs):
7385
"""
7486
return self.list(path="/snippets/public", **kwargs)
7587

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

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

85100
@cli.register_custom_action("ProjectSnippet")
86101
@exc.on_http_error(exc.GitlabGetError)
87-
def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
102+
def content(
103+
self,
104+
streamed: bool = False,
105+
action: Optional[Callable[..., Any]] = None,
106+
chunk_size: int = 1024,
107+
**kwargs: Any,
108+
) -> Optional[bytes]:
88109
"""Return the content of a snippet.
89110
90111
Args:
@@ -107,6 +128,8 @@ def content(self, streamed=False, action=None, chunk_size=1024, **kwargs):
107128
result = self.manager.gitlab.http_get(
108129
path, streamed=streamed, raw=True, **kwargs
109130
)
131+
if TYPE_CHECKING:
132+
assert isinstance(result, requests.Response)
110133
return utils.response_content(result, streamed, action, chunk_size)
111134

112135

@@ -121,3 +144,8 @@ class ProjectSnippetManager(CRUDMixin, RESTManager):
121144
_update_attrs = RequiredOptional(
122145
optional=("title", "file_name", "content", "visibility", "description"),
123146
)
147+
148+
def get(
149+
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
150+
) -> ProjectSnippet:
151+
return cast(ProjectSnippet, super().get(id=id, lazy=lazy, **kwargs))

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module = [
2424
"gitlab.v4.objects.repositories",
2525
"gitlab.v4.objects.services",
2626
"gitlab.v4.objects.sidekiq",
27-
"gitlab.v4.objects.snippets",
2827
"setup",
2928
"tests.functional.*",
3029
"tests.functional.api.*",

0 commit comments

Comments
 (0)