summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn L. Villalovos <john@sodarock.com>2021-03-07 09:16:45 -0800
committerJohn L. Villalovos <john@sodarock.com>2021-03-07 09:30:22 -0800
commit924f83eb4b5e160bd231efc38e2eea0231fa311f (patch)
tree2d66f0ad7b2accd1b584aafd55c22aaf595d660e
parent48fc907403b630f069dfd63fada73f96a8c6e983 (diff)
downloadgitlab-924f83eb4b5e160bd231efc38e2eea0231fa311f.tar.gz
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.
-rw-r--r--gitlab/base.py2
-rw-r--r--gitlab/mixins.py19
-rw-r--r--gitlab/v4/cli.py5
3 files changed, 12 insertions, 14 deletions
diff --git a/gitlab/base.py b/gitlab/base.py
index ed551ff..b9960ad 100644
--- a/gitlab/base.py
+++ b/gitlab/base.py
@@ -20,6 +20,7 @@ from types import ModuleType
from typing import Any, Dict, Optional, Type
from .client import Gitlab, GitlabList
+from gitlab import types as g_types
__all__ = [
"RESTObject",
@@ -260,6 +261,7 @@ class RESTManager(object):
_path: Optional[str] = None
_obj_cls: Optional[Type[RESTObject]] = None
_from_parent_attrs: Dict[str, Any] = {}
+ _types: Dict[str, Type[g_types.GitlabAttribute]] = {}
_computed_path: Optional[str]
_parent: Optional[RESTObject]
diff --git a/gitlab/mixins.py b/gitlab/mixins.py
index 520ce87..b2c246e 100644
--- a/gitlab/mixins.py
+++ b/gitlab/mixins.py
@@ -226,9 +226,8 @@ class ListMixin(_RestManagerBase):
data.setdefault("order_by", self.gitlab.order_by)
# We get the attributes that need some special transformation
- types = getattr(self, "_types", {})
- if types:
- for attr_name, type_cls in types.items():
+ if self._types:
+ for attr_name, type_cls in self._types.items():
if attr_name in data.keys():
type_obj = type_cls(data[attr_name])
data[attr_name] = type_obj.get_for_api()
@@ -311,17 +310,16 @@ class CreateMixin(_RestManagerBase):
files = {}
# We get the attributes that need some special transformation
- types = getattr(self, "_types", {})
- if types:
+ if self._types:
# Duplicate data to avoid messing with what the user sent us
data = data.copy()
- for attr_name, type_cls in types.items():
+ for attr_name, type_cls in self._types.items():
if attr_name in data.keys():
type_obj = type_cls(data[attr_name])
# if the type if FileAttribute we need to pass the data as
# file
- if issubclass(type_cls, g_types.FileAttribute):
+ if isinstance(type_obj, g_types.FileAttribute):
k = type_obj.get_file_name(attr_name)
files[attr_name] = (k, data.pop(attr_name))
else:
@@ -414,17 +412,16 @@ class UpdateMixin(_RestManagerBase):
files = {}
# We get the attributes that need some special transformation
- types = getattr(self, "_types", {})
- if types:
+ if self._types:
# Duplicate data to avoid messing with what the user sent us
new_data = new_data.copy()
- for attr_name, type_cls in types.items():
+ for attr_name, type_cls in self._types.items():
if attr_name in new_data.keys():
type_obj = type_cls(new_data[attr_name])
# if the type if FileAttribute we need to pass the data as
# file
- if issubclass(type_cls, g_types.FileAttribute):
+ if isinstance(type_obj, g_types.FileAttribute):
k = type_obj.get_file_name(attr_name)
files[attr_name] = (k, new_data.pop(attr_name))
else:
diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py
index 6172f93..a768999 100644
--- a/gitlab/v4/cli.py
+++ b/gitlab/v4/cli.py
@@ -42,9 +42,8 @@ class GitlabCLI(object):
self.mgr_cls._path = self.mgr_cls._path % self.args
self.mgr = self.mgr_cls(gl)
- types = getattr(self.mgr_cls, "_types", {})
- if types:
- for attr_name, type_cls in types.items():
+ if self.mgr_cls._types:
+ for attr_name, type_cls in self.mgr_cls._types.items():
if attr_name in self.args.keys():
obj = type_cls()
obj.set_from_cli(self.args[attr_name])