Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(api): Use protocols and overload to type hint mixins#3080

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

Closed
Show file tree
Hide file tree
Changes fromall 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
1 change: 0 additions & 1 deletiongitlab/base.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -347,7 +347,6 @@ class RESTManager:
_create_attrs: g_types.RequiredOptional = g_types.RequiredOptional()
_update_attrs: g_types.RequiredOptional = g_types.RequiredOptional()
_path: Optional[str] = None
_obj_cls: Optional[Type[RESTObject]] = None
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The_obj_cls needs to be specific for each class. Having a_obj_cls as None in base class will prevent Protocol from working.

_from_parent_attrs: Dict[str, Any] = {}
_types: Dict[str, Type[g_types.GitlabAttribute]] = {}

Expand Down
7 changes: 4 additions & 3 deletionsgitlab/client.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -397,10 +397,11 @@ def auth(self) -> None:
The `user` attribute will hold a `gitlab.objects.CurrentUser` object on
success.
"""
self.user = self._objects.CurrentUserManager(self).get()
user = self._objects.CurrentUserManager(self).get()
self.user = user
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

For some reasonself.user will now be type evaluated asOptional[User]. I had to separate user to a new variable that can't be None.


if hasattr(self.user, "web_url") and hasattr(self.user, "username"):
self._check_url(self.user.web_url, path=self.user.username)
if hasattr(user, "web_url") and hasattr(user, "username"):
self._check_url(user.web_url, path=user.username)

def version(self) -> Tuple[str, str]:
"""Returns the version and revision of the gitlab server.
Expand Down
115 changes: 106 additions & 9 deletionsgitlab/mixins.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import enum
from types import ModuleType
from typing import (
Expand All@@ -6,10 +8,14 @@
Dict,
Iterator,
List,
Literal,
Optional,
overload,
Protocol,
Tuple,
Type,
TYPE_CHECKING,
TypeVar,
Union,
)

Expand DownExpand Up@@ -52,6 +58,12 @@
_RestManagerBase = object
_RestObjectBase = object

TObjCls = TypeVar("TObjCls", bound=base.RESTObject)


class ObjClsProtocol(Protocol[TObjCls]):
_obj_cls: Type[TObjCls]


class HeadMixin(_RestManagerBase):
@exc.on_http_error(exc.GitlabHeadError)
Expand DownExpand Up@@ -84,13 +96,29 @@ def head(
class GetMixin(HeadMixin, _RestManagerBase):
_computed_path: Optional[str]
_from_parent_attrs: Dict[str, Any]
_obj_cls: Optional[Type[base.RESTObject]]
_optional_get_attrs: Tuple[str, ...] = ()
_obj_cls: Type[base.RESTObject]
_parent: Optional[base.RESTObject]
_parent_attrs: Dict[str, Any]
_path: Optional[str]
gitlab: gitlab.Gitlab

@overload
def get(
self: ObjClsProtocol[TObjCls],
id: Union[str, int],
lazy: bool = False,
**kwargs: Any,
) -> TObjCls: ...

@overload
def get(
self: Any,
id: Union[str, int],
lazy: bool = False,
**kwargs: Any,
) -> base.RESTObject: ...

@exc.on_http_error(exc.GitlabGetError)
def get(
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
Expand DownExpand Up@@ -129,13 +157,19 @@ def get(
class GetWithoutIdMixin(HeadMixin, _RestManagerBase):
_computed_path: Optional[str]
_from_parent_attrs: Dict[str, Any]
_obj_cls: Optional[Type[base.RESTObject]]
_optional_get_attrs: Tuple[str, ...] = ()
_obj_cls: Type[base.RESTObject]
_parent: Optional[base.RESTObject]
_parent_attrs: Dict[str, Any]
_path: Optional[str]
gitlab: gitlab.Gitlab

@overload
def get(self: ObjClsProtocol[TObjCls], **kwargs: Any) -> TObjCls: ...

@overload
def get(self: Any, **kwargs: Any) -> base.RESTObject: ...

@exc.on_http_error(exc.GitlabGetError)
def get(self, **kwargs: Any) -> base.RESTObject:
"""Retrieve a single object.
Expand DownExpand Up@@ -196,14 +230,54 @@ class ListMixin(HeadMixin, _RestManagerBase):
_computed_path: Optional[str]
_from_parent_attrs: Dict[str, Any]
_list_filters: Tuple[str, ...] = ()
_obj_cls:Optional[Type[base.RESTObject]]
_obj_cls: Type[base.RESTObject]
_parent: Optional[base.RESTObject]
_parent_attrs: Dict[str, Any]
_path: Optional[str]
gitlab: gitlab.Gitlab

@overload
def list(
self: ObjClsProtocol[TObjCls],
*,
iterator: Literal[False] = False,
**kwargs: Any,
) -> List[TObjCls]: ...

@overload
def list(
self: ObjClsProtocol[TObjCls],
*,
iterator: Literal[True] = True,
**kwargs: Any,
) -> base.RESTObjectList: ...
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I added an overload to the list method so that passingiterator will change if a list is returned or an iterator. This can be separated to another commit and MR.

AlsoRESTObjectList is not generic. I want to create a follow-up MR there it will be made generic to iterate over a specific type.


@overload
def list(
self: Any,
*,
iterator: Literal[False] = False,
**kwargs: Any,
) -> List[base.RESTObject]: ...

@overload
def list(
self: Any,
*,
iterator: Literal[True] = True,
**kwargs: Any,
) -> base.RESTObjectList: ...

@overload
def list(
self: Any,
**kwargs: Any,
) -> Union[base.RESTObjectList, List[base.RESTObject]]: ...

@exc.on_http_error(exc.GitlabListError)
def list(self, **kwargs: Any) -> Union[base.RESTObjectList, List[base.RESTObject]]:
def list(
self, *, iterator: bool = False, **kwargs: Any
) -> Union[base.RESTObjectList, List[Any]]:
"""Retrieve a list of objects.

Args:
Expand All@@ -221,6 +295,7 @@ def list(self, **kwargs: Any) -> Union[base.RESTObjectList, List[base.RESTObject
GitlabAuthenticationError: If authentication is not correct
GitlabListError: If the server cannot perform the request
"""
kwargs.update(iterator=iterator)

data, _ = utils._transform_types(
data=kwargs,
Expand DownExpand Up@@ -253,7 +328,6 @@ def list(self, **kwargs: Any) -> Union[base.RESTObjectList, List[base.RESTObject
class RetrieveMixin(ListMixin, GetMixin):
_computed_path: Optional[str]
_from_parent_attrs: Dict[str, Any]
_obj_cls: Optional[Type[base.RESTObject]]
_parent: Optional[base.RESTObject]
_parent_attrs: Dict[str, Any]
_path: Optional[str]
Expand All@@ -263,12 +337,24 @@ class RetrieveMixin(ListMixin, GetMixin):
class CreateMixin(_RestManagerBase):
_computed_path: Optional[str]
_from_parent_attrs: Dict[str, Any]
_obj_cls:Optional[Type[base.RESTObject]]
_obj_cls: Type[base.RESTObject]
_parent: Optional[base.RESTObject]
_parent_attrs: Dict[str, Any]
_path: Optional[str]
gitlab: gitlab.Gitlab

@overload
def create(
self: ObjClsProtocol[TObjCls],
data: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> TObjCls: ...

@overload
def create(
self: Any, data: Optional[Dict[str, Any]] = None, **kwargs: Any
) -> base.RESTObject: ...

@exc.on_http_error(exc.GitlabCreateError)
def create(
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
Expand DownExpand Up@@ -385,12 +471,23 @@ def update(
class SetMixin(_RestManagerBase):
_computed_path: Optional[str]
_from_parent_attrs: Dict[str, Any]
_obj_cls:Optional[Type[base.RESTObject]]
_obj_cls: Type[base.RESTObject]
_parent: Optional[base.RESTObject]
_parent_attrs: Dict[str, Any]
_path: Optional[str]
gitlab: gitlab.Gitlab

@overload
def set(
self: ObjClsProtocol[TObjCls],
key: str,
value: str,
**kwargs: Any,
) -> TObjCls: ...

@overload
def set(self: Any, key: str, value: str, **kwargs: Any) -> base.RESTObject: ...

@exc.on_http_error(exc.GitlabSetError)
def set(self, key: str, value: str, **kwargs: Any) -> base.RESTObject:
"""Create or update the object.
Expand DownExpand Up@@ -450,7 +547,7 @@ def delete(self, id: Optional[Union[str, int]] = None, **kwargs: Any) -> None:
class CRUDMixin(GetMixin, ListMixin, CreateMixin, UpdateMixin, DeleteMixin):
_computed_path: Optional[str]
_from_parent_attrs: Dict[str, Any]
_obj_cls:Optional[Type[base.RESTObject]]
_obj_cls: Type[base.RESTObject]
_parent: Optional[base.RESTObject]
_parent_attrs: Dict[str, Any]
_path: Optional[str]
Expand All@@ -460,7 +557,7 @@ class CRUDMixin(GetMixin, ListMixin, CreateMixin, UpdateMixin, DeleteMixin):
class NoUpdateMixin(GetMixin, ListMixin, CreateMixin, DeleteMixin):
_computed_path: Optional[str]
_from_parent_attrs: Dict[str, Any]
_obj_cls:Optional[Type[base.RESTObject]]
_obj_cls: Type[base.RESTObject]
_parent: Optional[base.RESTObject]
_parent_attrs: Dict[str, Any]
_path: Optional[str]
Expand Down
2 changes: 1 addition & 1 deletiongitlab/v4/cli.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -401,7 +401,7 @@ def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
if not isinstance(cls, type):
continue
if issubclass(cls, gitlab.base.RESTManager):
if cls._obj_cls is not None:
ifhasattr(cls, "_obj_cls"):
classes.add(cls._obj_cls)

for cls in sorted(classes, key=operator.attrgetter("__name__")):
Expand Down
5 changes: 1 addition & 4 deletionsgitlab/v4/objects/appearance.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
from typing import Any,cast,Dict, Optional, Union
from typing import Any, Dict, Optional, Union

from gitlab import exceptions as exc
from gitlab.base import RESTManager, RESTObject
Expand DownExpand Up@@ -58,6 +58,3 @@ def update(
new_data = new_data or {}
data = new_data.copy()
return super().update(id, data, **kwargs)

def get(self, **kwargs: Any) -> ApplicationAppearance:
return cast(ApplicationAppearance, super().get(**kwargs))
15 changes: 0 additions & 15 deletionsgitlab/v4/objects/audit_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,8 +3,6 @@
https://docs.gitlab.com/ee/api/audit_events.html
"""

from typing import Any, cast, Union

from gitlab.base import RESTManager, RESTObject
from gitlab.mixins import RetrieveMixin

Expand All@@ -29,9 +27,6 @@ class AuditEventManager(RetrieveMixin, RESTManager):
_obj_cls = AuditEvent
_list_filters = ("created_after", "created_before", "entity_type", "entity_id")

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


class GroupAuditEvent(RESTObject):
_id_attr = "id"
Expand All@@ -43,11 +38,6 @@ class GroupAuditEventManager(RetrieveMixin, RESTManager):
_from_parent_attrs = {"group_id": "id"}
_list_filters = ("created_after", "created_before")

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


class ProjectAuditEvent(RESTObject):
_id_attr = "id"
Expand All@@ -59,11 +49,6 @@ class ProjectAuditEventManager(RetrieveMixin, RESTManager):
_from_parent_attrs = {"project_id": "id"}
_list_filters = ("created_after", "created_before")

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


class ProjectAudit(ProjectAuditEvent):
pass
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp