Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd64572

Browse files
committed
Remove duplicate type definitions in middle-level abstract classes
1 parent de3a1b6 commit bd64572

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

gitlab/base.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,10 @@ def total(self) -> Optional[int]:
328328
"""The total number of items."""
329329
return self._list.total
330330

331+
T_obj = TypeVar("T_obj", bound=RESTObject)
332+
T_parent = TypeVar("T_parent", bound=Optional[RESTObject])
331333

332-
class RESTManager:
334+
class RESTManager(Generic[T_obj, T_parent]):
333335
"""Base class for CRUD operations on objects.
334336
335337
Derived class must define ``_path`` and ``_obj_cls``.
@@ -341,16 +343,16 @@ class RESTManager:
341343
_create_attrs: g_types.RequiredOptional = g_types.RequiredOptional()
342344
_update_attrs: g_types.RequiredOptional = g_types.RequiredOptional()
343345
_path: Optional[str] = None
344-
_obj_cls: Optional[Type[RESTObject]] = None
346+
_obj_cls: Type[T_obj]
345347
_from_parent_attrs: Dict[str, Any] = {}
346348
_types: Dict[str, Type[g_types.GitlabAttribute]] = {}
347349

348350
_computed_path: Optional[str]
349-
_parent: Optional[RESTObject]
351+
_parent: Optional[T_parent]
350352
_parent_attrs: Dict[str, Any]
351353
gitlab: Gitlab
352354

353-
def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
355+
def __init__(self, gl: Gitlab, parent: Optional[T_parent] = None) -> None:
354356
"""REST manager constructor.
355357
356358
Args:

gitlab/mixins.py

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,22 @@
7070
_RestManagerBase = object
7171
_RestObjectBase = object
7272

73-
T1 = TypeVar("T1", bound=base.RESTObject)
74-
T2 = TypeVar("T2", bound=base.RESTObject)
73+
T_obj = TypeVar("T_obj", bound=base.RESTObject)
74+
T_parent = TypeVar("T_parent", bound=Optional[base.RESTObject])
7575

7676

77-
class GetMixin(_RestManagerBase, Generic[T1, T2]):
77+
class GetMixin(_RestManagerBase[T_obj, T_parent], Generic[T_obj, T_parent]):
7878
_computed_path: Optional[str]
7979
_from_parent_attrs: Dict[str, Any]
80-
_obj_cls: Type[T1]
8180
_optional_get_attrs: Tuple[str, ...] = ()
82-
_parent: Optional[T2]
8381
_parent_attrs: Dict[str, Any]
8482
_path: Optional[str]
8583
gitlab: gitlab.Gitlab
8684

8785
@exc.on_http_error(exc.GitlabGetError)
8886
def get(
8987
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
90-
) -> T1:
88+
) -> T_obj:
9189
"""Retrieve a single object.
9290
9391
Args:
@@ -119,12 +117,10 @@ def get(
119117
return self._obj_cls(self, server_data)
120118

121119

122-
class GetWithoutIdMixin(_RestManagerBase, Generic[T1, T2]):
120+
class GetWithoutIdMixin(_RestManagerBase[T_obj, T_parent], Generic[T_obj, T_parent]):
123121
_computed_path: Optional[str]
124122
_from_parent_attrs: Dict[str, Any]
125-
_obj_cls: Type[T1]
126123
_optional_get_attrs: Tuple[str, ...] = ()
127-
_parent: Optional[T2]
128124
_parent_attrs: Dict[str, Any]
129125
_path: Optional[str]
130126
gitlab: gitlab.Gitlab
@@ -187,18 +183,16 @@ def refresh(self, **kwargs: Any) -> None:
187183
self._update_attrs(server_data)
188184

189185

190-
class ListMixin(_RestManagerBase, Generic[T1, T2]):
186+
class ListMixin(_RestManagerBase[T_obj, T_parent], Generic[T_obj, T_parent]):
191187
_computed_path: Optional[str]
192188
_from_parent_attrs: Dict[str, Any]
193189
_list_filters: Tuple[str, ...] = ()
194-
_obj_cls: Type[T1]
195-
_parent: Optional[T2]
196190
_parent_attrs: Dict[str, Any]
197191
_path: Optional[str]
198192
gitlab: gitlab.Gitlab
199193

200194
@exc.on_http_error(exc.GitlabListError)
201-
def list(self, **kwargs: Any) -> Iterable[T1]:
195+
def list(self, **kwargs: Any) -> Iterable[T_obj]:
202196
"""Retrieve a list of objects.
203197
204198
Args:
@@ -240,21 +234,17 @@ def list(self, **kwargs: Any) -> Iterable[T1]:
240234
return base.RESTObjectList(self, self._obj_cls, obj)
241235

242236

243-
class RetrieveMixin(ListMixin, GetMixin, Generic[T1, T2]):
237+
class RetrieveMixin(ListMixin[T_obj, T_parent], GetMixin[T_obj, T_parent], Generic[T_obj, T_parent]):
244238
_computed_path: Optional[str]
245239
_from_parent_attrs: Dict[str, Any]
246-
_obj_cls: Type[T1]
247-
_parent: Optional[T2]
248240
_parent_attrs: Dict[str, Any]
249241
_path: Optional[str]
250242
gitlab: gitlab.Gitlab
251243

252244

253-
class CreateMixin(_RestManagerBase, Generic[T1, T2]):
245+
class CreateMixin(_RestManagerBase[T_obj, T_parent], Generic[T_obj, T_parent]):
254246
_computed_path: Optional[str]
255247
_from_parent_attrs: Dict[str, Any]
256-
_obj_cls: Type[T1]
257-
_parent: Optional[T2]
258248
_parent_attrs: Dict[str, Any]
259249
_path: Optional[str]
260250
gitlab: gitlab.Gitlab
@@ -293,11 +283,9 @@ def create(
293283
return self._obj_cls(self, server_data)
294284

295285

296-
class UpdateMixin(_RestManagerBase, Generic[T1, T2]):
286+
class UpdateMixin(_RestManagerBase[T_obj, T_parent], Generic[T_obj, T_parent]):
297287
_computed_path: Optional[str]
298288
_from_parent_attrs: Dict[str, Any]
299-
_obj_cls: Type[T1]
300-
_parent: Optional[T2]
301289
_parent_attrs: Dict[str, Any]
302290
_path: Optional[str]
303291
_update_uses_post: bool = False
@@ -358,11 +346,9 @@ def update(
358346
return result
359347

360348

361-
class SetMixin(_RestManagerBase, Generic[T1, T2]):
349+
class SetMixin(_RestManagerBase[T_obj, T_parent], Generic[T_obj, T_parent]):
362350
_computed_path: Optional[str]
363351
_from_parent_attrs: Dict[str, Any]
364-
_obj_cls: Type[T1]
365-
_parent: Optional[T2]
366352
_parent_attrs: Dict[str, Any]
367353
_path: Optional[str]
368354
gitlab: gitlab.Gitlab
@@ -392,11 +378,9 @@ def set(self, key: str, value: str, **kwargs: Any) -> base.RESTObject:
392378
return self._obj_cls(self, server_data)
393379

394380

395-
class DeleteMixin(_RestManagerBase, Generic[T1, T2]):
381+
class DeleteMixin(_RestManagerBase[T_obj, T_parent], Generic[T_obj, T_parent]):
396382
_computed_path: Optional[str]
397383
_from_parent_attrs: Dict[str, Any]
398-
_obj_cls: Type[T1]
399-
_parent: Optional[T2]
400384
_parent_attrs: Dict[str, Any]
401385
_path: Optional[str]
402386
gitlab: gitlab.Gitlab
@@ -423,21 +407,17 @@ def delete(self, id: Optional[Union[str, int]] = None, **kwargs: Any) -> None:
423407
self.gitlab.http_delete(path, **kwargs)
424408

425409

426-
class CRUDMixin(GetMixin, ListMixin, CreateMixin, UpdateMixin, DeleteMixin, Generic[T1, T2]):
410+
class CRUDMixin(GetMixin[T_obj, T_parent], ListMixin[T_obj, T_parent], CreateMixin[T_obj, T_parent], UpdateMixin[T_obj, T_parent], DeleteMixin[T_obj, T_parent], Generic[T_obj, T_parent]):
427411
_computed_path: Optional[str]
428412
_from_parent_attrs: Dict[str, Any]
429-
_obj_cls: Type[T1]
430-
_parent: Optional[T2]
431413
_parent_attrs: Dict[str, Any]
432414
_path: Optional[str]
433415
gitlab: gitlab.Gitlab
434416

435417

436-
class NoUpdateMixin(GetMixin, ListMixin, CreateMixin, DeleteMixin, Generic[T1, T2]):
418+
class NoUpdateMixin(GetMixin[T_obj, T_parent], ListMixin[T_obj, T_parent], CreateMixin[T_obj, T_parent], DeleteMixin[T_obj, T_parent], Generic[T_obj, T_parent]):
437419
_computed_path: Optional[str]
438420
_from_parent_attrs: Dict[str, Any]
439-
_obj_cls: Type[T1]
440-
_parent: T2
441421
_parent_attrs: Dict[str, Any]
442422
_path: Optional[str]
443423
gitlab: gitlab.Gitlab
@@ -838,7 +818,7 @@ def participants(self, **kwargs: Any) -> Dict[str, Any]:
838818
return result
839819

840820

841-
class BadgeRenderMixin(_RestManagerBase):
821+
class BadgeRenderMixin(_RestManagerBase[T_obj, T_parent]):
842822
@cli.register_custom_action(
843823
("GroupBadgeManager", "ProjectBadgeManager"), ("link_url", "image_url")
844824
)

0 commit comments

Comments
 (0)