Skip to content

chore: create return type-hints for get_id() & encoded_id #1841

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
Jan 15, 2022
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
6 changes: 3 additions & 3 deletions gitlab/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pprint
import textwrap
from types import ModuleType
from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type
from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type, Union

import gitlab
from gitlab import types as g_types
Expand Down Expand Up @@ -211,14 +211,14 @@ def _update_attrs(self, new_attrs: Dict[str, Any]) -> None:
self.__dict__["_updated_attrs"] = {}
self.__dict__["_attrs"] = new_attrs

def get_id(self) -> Any:
def get_id(self) -> Optional[Union[int, str]]:
"""Returns the id of the resource."""
if self._id_attr is None or not hasattr(self, self._id_attr):
return None
return getattr(self, self._id_attr)

@property
def encoded_id(self) -> Any:
def encoded_id(self) -> Optional[Union[int, str]]:
"""Ensure that the ID is url-encoded so that it can be safely used in a URL
path"""
obj_id = self.get_id()
Expand Down
1 change: 1 addition & 0 deletions gitlab/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ def delete(self, **kwargs: Any) -> None:
"""
if TYPE_CHECKING:
assert isinstance(self.manager, DeleteMixin)
assert self.encoded_id is not None
self.manager.delete(self.encoded_id, **kwargs)


Expand Down
2 changes: 2 additions & 0 deletions gitlab/v4/objects/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def delete( # type: ignore
GitlabDeleteError: If the server cannot perform the request
"""
file_path = self.encoded_id
if TYPE_CHECKING:
assert isinstance(file_path, str)
self.manager.delete(file_path, branch, commit_message, **kwargs)


Expand Down