- Notifications
You must be signed in to change notification settings - Fork673
chore: add type-hints to remaining gitlab/v4/objects/*.py files#1695
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
9a451a8
chore: fix issue with adding type-hints to 'manager' attribute
JohnVillalovosd4adf8d
chore: add type-hints to gitlab/v4/objects/epics.py
JohnVillalovos13243b7
chore: add type-hints to gitlab/v4/objects/geo_nodes.py
JohnVillalovos93e39a2
chore: add type-hints to gitlab/v4/objects/issues.py
JohnVillalovose8884f2
chore: add type-hints to gitlab/v4/objects/jobs.py
JohnVillalovos8b6078f
chore: add type-hints to gitlab/v4/objects/milestones.py
JohnVillalovoscb3ad6c
chore: add type-hints to gitlab/v4/objects/pipelines.py
JohnVillalovos00d7b20
chore: add type-hints to gitlab/v4/objects/repositories.py
JohnVillalovos8da0b75
chore: add type-hints to gitlab/v4/objects/services.py
JohnVillalovosa91a303
chore: add type-hints to gitlab/v4/objects/sidekiq.py
JohnVillalovosd04e557
chore: add type-hints to gitlab/v4/objects/labels.py
JohnVillalovos0c22bd9
chore: add type-hints to gitlab/v4/objects/files.py
JohnVillalovosFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletionsgitlab/base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -150,6 +150,10 @@ def _create_managers(self) -> None: | ||
# annotations. If an attribute is annotated as being a *Manager type | ||
# then we create the manager and assign it to the attribute. | ||
for attr, annotation in sorted(self.__annotations__.items()): | ||
# We ignore creating a manager for the 'manager' attribute as that | ||
# is done in the self.__init__() method | ||
if attr in ("manager",): | ||
continue | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if not isinstance(annotation, (type, str)): | ||
continue | ||
if isinstance(annotation, type): | ||
18 changes: 16 additions & 2 deletionsgitlab/v4/objects/epics.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from typing import Any, cast, Dict, Optional, TYPE_CHECKING, Union | ||
from gitlab import exceptions as exc | ||
from gitlab import types | ||
from gitlab.base import RequiredOptional, RESTManager, RESTObject | ||
@@ -42,11 +44,17 @@ class GroupEpicManager(CRUDMixin, RESTManager): | ||
) | ||
_types = {"labels": types.ListAttribute} | ||
def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> GroupEpic: | ||
return cast(GroupEpic, super().get(id=id, lazy=lazy, **kwargs)) | ||
class GroupEpicIssue(ObjectDeleteMixin, SaveMixin, RESTObject): | ||
_id_attr = "epic_issue_id" | ||
# Define type for 'manager' here So mypy won't complain about | ||
# 'self.manager.update()' call in the 'save' method. | ||
manager: "GroupEpicIssueManager" | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def save(self, **kwargs: Any) -> None: | ||
"""Save the changes made to the object to the server. | ||
The object is updated to match what the server returns. | ||
@@ -78,7 +86,9 @@ class GroupEpicIssueManager( | ||
_update_attrs = RequiredOptional(optional=("move_before_id", "move_after_id")) | ||
@exc.on_http_error(exc.GitlabCreateError) | ||
def create( | ||
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any | ||
) -> GroupEpicIssue: | ||
"""Create a new object. | ||
Args: | ||
@@ -94,9 +104,13 @@ def create(self, data, **kwargs): | ||
RESTObject: A new instance of the manage object class build with | ||
the data sent by the server | ||
""" | ||
if TYPE_CHECKING: | ||
assert data is not None | ||
CreateMixin._check_missing_create_attrs(self, data) | ||
path = f"{self.path}/{data.pop('issue_id')}" | ||
server_data = self.gitlab.http_post(path, **kwargs) | ||
if TYPE_CHECKING: | ||
assert isinstance(server_data, dict) | ||
# The epic_issue_id attribute doesn't exist when creating the resource, | ||
# but is used everywhere elese. Let's create it to be consistent client | ||
# side | ||
70 changes: 58 additions & 12 deletionsgitlab/v4/objects/files.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import base64 | ||
from typing import Any, Callable, cast, Dict, List, Optional, TYPE_CHECKING | ||
import requests | ||
from gitlab import cli | ||
from gitlab import exceptions as exc | ||
@@ -22,6 +25,8 @@ | ||
class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject): | ||
_id_attr = "file_path" | ||
_short_print_attr = "file_path" | ||
file_path: str | ||
manager: "ProjectFileManager" | ||
def decode(self) -> bytes: | ||
"""Returns the decoded content of the file. | ||
@@ -31,7 +36,11 @@ def decode(self) -> bytes: | ||
""" | ||
return base64.b64decode(self.content) | ||
# NOTE(jlvillal): Signature doesn't match SaveMixin.save() so ignore | ||
# type error | ||
def save( # type: ignore | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
self, branch: str, commit_message: str, **kwargs: Any | ||
) -> None: | ||
"""Save the changes made to the file to the server. | ||
The object is updated to match what the server returns. | ||
@@ -50,7 +59,12 @@ def save(self, branch, commit_message, **kwargs): | ||
self.file_path = self.file_path.replace("/", "%2F") | ||
super(ProjectFile, self).save(**kwargs) | ||
@exc.on_http_error(exc.GitlabDeleteError) | ||
# NOTE(jlvillal): Signature doesn't match DeleteMixin.delete() so ignore | ||
# type error | ||
def delete( # type: ignore | ||
self, branch: str, commit_message: str, **kwargs: Any | ||
) -> None: | ||
"""Delete the file from the server. | ||
Args: | ||
@@ -80,7 +94,11 @@ class ProjectFileManager(GetMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTMa | ||
) | ||
@cli.register_custom_action("ProjectFileManager", ("file_path", "ref")) | ||
# NOTE(jlvillal): Signature doesn't match UpdateMixin.update() so ignore | ||
# type error | ||
def get( # type: ignore | ||
self, file_path: str, ref: str, **kwargs: Any | ||
) -> ProjectFile: | ||
"""Retrieve a single file. | ||
Args: | ||
@@ -95,15 +113,17 @@ def get(self, file_path, ref, **kwargs): | ||
Returns: | ||
object: The generated RESTObject | ||
""" | ||
returncast(ProjectFile,GetMixin.get(self, file_path, ref=ref, **kwargs)) | ||
@cli.register_custom_action( | ||
"ProjectFileManager", | ||
("file_path", "branch", "content", "commit_message"), | ||
("encoding", "author_email", "author_name"), | ||
) | ||
@exc.on_http_error(exc.GitlabCreateError) | ||
def create( | ||
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any | ||
) -> ProjectFile: | ||
"""Create a new object. | ||
Args: | ||
@@ -120,15 +140,23 @@ def create(self, data, **kwargs): | ||
GitlabCreateError: If the server cannot perform the request | ||
""" | ||
if TYPE_CHECKING: | ||
assert data is not None | ||
self._check_missing_create_attrs(data) | ||
new_data = data.copy() | ||
file_path = new_data.pop("file_path").replace("/", "%2F") | ||
path = f"{self.path}/{file_path}" | ||
server_data = self.gitlab.http_post(path, post_data=new_data, **kwargs) | ||
if TYPE_CHECKING: | ||
assert isinstance(server_data, dict) | ||
return self._obj_cls(self, server_data) | ||
@exc.on_http_error(exc.GitlabUpdateError) | ||
# NOTE(jlvillal): Signature doesn't match UpdateMixin.update() so ignore | ||
# type error | ||
def update( # type: ignore | ||
self, file_path: str, new_data: Optional[Dict[str, Any]] = None, **kwargs: Any | ||
) -> Dict[str, Any]: | ||
"""Update an object on the server. | ||
Args: | ||
@@ -149,13 +177,20 @@ def update(self, file_path, new_data=None, **kwargs): | ||
data["file_path"] = file_path | ||
path = f"{self.path}/{file_path}" | ||
self._check_missing_update_attrs(data) | ||
result = self.gitlab.http_put(path, post_data=data, **kwargs) | ||
if TYPE_CHECKING: | ||
assert isinstance(result, dict) | ||
return result | ||
@cli.register_custom_action( | ||
"ProjectFileManager", ("file_path", "branch", "commit_message") | ||
) | ||
@exc.on_http_error(exc.GitlabDeleteError) | ||
# NOTE(jlvillal): Signature doesn't match DeleteMixin.delete() so ignore | ||
# type error | ||
def delete( # type: ignore | ||
self, file_path: str, branch: str, commit_message: str, **kwargs: Any | ||
) -> None: | ||
"""Delete a file on the server. | ||
Args: | ||
@@ -175,8 +210,14 @@ def delete(self, file_path, branch, commit_message, **kwargs): | ||
@cli.register_custom_action("ProjectFileManager", ("file_path", "ref")) | ||
@exc.on_http_error(exc.GitlabGetError) | ||
def raw( | ||
self, | ||
file_path: str, | ||
ref: str, | ||
streamed: bool = False, | ||
action: Optional[Callable[..., Any]] = None, | ||
chunk_size: int = 1024, | ||
**kwargs: Any, | ||
) -> Optional[bytes]: | ||
"""Return the content of a file for a commit. | ||
Args: | ||
@@ -203,11 +244,13 @@ def raw( | ||
result = self.gitlab.http_get( | ||
path, query_data=query_data, streamed=streamed, raw=True, **kwargs | ||
) | ||
if TYPE_CHECKING: | ||
assert isinstance(result, requests.Response) | ||
return utils.response_content(result, streamed, action, chunk_size) | ||
@cli.register_custom_action("ProjectFileManager", ("file_path", "ref")) | ||
@exc.on_http_error(exc.GitlabListError) | ||
def blame(self, file_path: str, ref: str, **kwargs: Any) -> List[Dict[str, Any]]: | ||
"""Return the content of a file for a commit. | ||
Args: | ||
@@ -225,4 +268,7 @@ def blame(self, file_path, ref, **kwargs): | ||
file_path = file_path.replace("/", "%2F").replace(".", "%2E") | ||
path = f"{self.path}/{file_path}/blame" | ||
query_data = {"ref": ref} | ||
result = self.gitlab.http_list(path, query_data, **kwargs) | ||
if TYPE_CHECKING: | ||
assert isinstance(result, list) | ||
return result |
30 changes: 23 additions & 7 deletionsgitlab/v4/objects/geo_nodes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.