Skip to content

Commit 924f83e

Browse files
chore: make _types always present in RESTManager
We now create _types = {} in RESTManager class. By making _types always present in RESTManager it makes the code simpler. We no longer have to do: types = getattr(self, "_types", {}) And the type checker now understands the type.
1 parent 48fc907 commit 924f83e

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

gitlab/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing import Any, Dict, Optional, Type
2121

2222
from .client import Gitlab, GitlabList
23+
from gitlab import types as g_types
2324

2425
__all__ = [
2526
"RESTObject",
@@ -260,6 +261,7 @@ class RESTManager(object):
260261
_path: Optional[str] = None
261262
_obj_cls: Optional[Type[RESTObject]] = None
262263
_from_parent_attrs: Dict[str, Any] = {}
264+
_types: Dict[str, Type[g_types.GitlabAttribute]] = {}
263265

264266
_computed_path: Optional[str]
265267
_parent: Optional[RESTObject]

gitlab/mixins.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,8 @@ def list(self, **kwargs: Any) -> Union[base.RESTObjectList, List[base.RESTObject
226226
data.setdefault("order_by", self.gitlab.order_by)
227227

228228
# We get the attributes that need some special transformation
229-
types = getattr(self, "_types", {})
230-
if types:
231-
for attr_name, type_cls in types.items():
229+
if self._types:
230+
for attr_name, type_cls in self._types.items():
232231
if attr_name in data.keys():
233232
type_obj = type_cls(data[attr_name])
234233
data[attr_name] = type_obj.get_for_api()
@@ -311,17 +310,16 @@ def create(
311310
files = {}
312311

313312
# We get the attributes that need some special transformation
314-
types = getattr(self, "_types", {})
315-
if types:
313+
if self._types:
316314
# Duplicate data to avoid messing with what the user sent us
317315
data = data.copy()
318-
for attr_name, type_cls in types.items():
316+
for attr_name, type_cls in self._types.items():
319317
if attr_name in data.keys():
320318
type_obj = type_cls(data[attr_name])
321319

322320
# if the type if FileAttribute we need to pass the data as
323321
# file
324-
if issubclass(type_cls, g_types.FileAttribute):
322+
if isinstance(type_obj, g_types.FileAttribute):
325323
k = type_obj.get_file_name(attr_name)
326324
files[attr_name] = (k, data.pop(attr_name))
327325
else:
@@ -414,17 +412,16 @@ def update(
414412
files = {}
415413

416414
# We get the attributes that need some special transformation
417-
types = getattr(self, "_types", {})
418-
if types:
415+
if self._types:
419416
# Duplicate data to avoid messing with what the user sent us
420417
new_data = new_data.copy()
421-
for attr_name, type_cls in types.items():
418+
for attr_name, type_cls in self._types.items():
422419
if attr_name in new_data.keys():
423420
type_obj = type_cls(new_data[attr_name])
424421

425422
# if the type if FileAttribute we need to pass the data as
426423
# file
427-
if issubclass(type_cls, g_types.FileAttribute):
424+
if isinstance(type_obj, g_types.FileAttribute):
428425
k = type_obj.get_file_name(attr_name)
429426
files[attr_name] = (k, new_data.pop(attr_name))
430427
else:

gitlab/v4/cli.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ def __init__(self, gl, what, action, args):
4242
self.mgr_cls._path = self.mgr_cls._path % self.args
4343
self.mgr = self.mgr_cls(gl)
4444

45-
types = getattr(self.mgr_cls, "_types", {})
46-
if types:
47-
for attr_name, type_cls in types.items():
45+
if self.mgr_cls._types:
46+
for attr_name, type_cls in self.mgr_cls._types.items():
4847
if attr_name in self.args.keys():
4948
obj = type_cls()
5049
obj.set_from_cli(self.args[attr_name])

0 commit comments

Comments
 (0)