summaryrefslogtreecommitdiff
path: root/gitlab/mixins.py
diff options
context:
space:
mode:
authorJohn L. Villalovos <john@sodarock.com>2021-03-07 11:31:23 -0800
committerJohn L. Villalovos <john@sodarock.com>2021-03-10 08:37:07 -0800
commit147f05d43d302d9a04bc87d957c79ce9e54cdaed (patch)
treebb639917a30dbb3de31bf6e47ea022794626bc1c /gitlab/mixins.py
parent6fde2437e82aeb8af903f81e351790b4695074a1 (diff)
downloadgitlab-147f05d43d302d9a04bc87d957c79ce9e54cdaed.tar.gz
chore: add _create_attrs & _update_attrs to RESTManager
Add the attributes: _create_attrs and _update_attrs to the RESTManager class. This is so that we stop using getattr() if we don't need to. This also helps with type-hints being available for these attributes.
Diffstat (limited to 'gitlab/mixins.py')
-rw-r--r--gitlab/mixins.py32
1 files changed, 7 insertions, 25 deletions
diff --git a/gitlab/mixins.py b/gitlab/mixins.py
index b2c246e..fd77904 100644
--- a/gitlab/mixins.py
+++ b/gitlab/mixins.py
@@ -266,24 +266,14 @@ class CreateMixin(_RestManagerBase):
gitlab: gitlab.Gitlab
def _check_missing_create_attrs(self, data: Dict[str, Any]) -> None:
- required, optional = self.get_create_attrs()
missing = []
- for attr in required:
+ for attr in self._create_attrs[0]:
if attr not in data:
missing.append(attr)
continue
if missing:
raise AttributeError("Missing attributes: %s" % ", ".join(missing))
- def get_create_attrs(self) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
- """Return the required and optional arguments.
-
- Returns:
- tuple: 2 items: list of required arguments and list of optional
- arguments for creation (in that order)
- """
- return getattr(self, "_create_attrs", (tuple(), tuple()))
-
@exc.on_http_error(exc.GitlabCreateError)
def create(
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
@@ -344,11 +334,13 @@ class UpdateMixin(_RestManagerBase):
gitlab: gitlab.Gitlab
def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None:
- required, optional = self.get_update_attrs()
if TYPE_CHECKING:
assert self._obj_cls is not None
- # Remove the id field from the required list as it was previously moved to the http path.
- required = tuple([k for k in required if k != self._obj_cls._id_attr])
+ # Remove the id field from the required list as it was previously moved
+ # to the http path.
+ required = tuple(
+ [k for k in self._update_attrs[0] if k != self._obj_cls._id_attr]
+ )
missing = []
for attr in required:
if attr not in data:
@@ -357,15 +349,6 @@ class UpdateMixin(_RestManagerBase):
if missing:
raise AttributeError("Missing attributes: %s" % ", ".join(missing))
- def get_update_attrs(self) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
- """Return the required and optional arguments.
-
- Returns:
- tuple: 2 items: list of required arguments and list of optional
- arguments for update (in that order)
- """
- return getattr(self, "_update_attrs", (tuple(), tuple()))
-
def _get_update_method(
self,
) -> Callable[..., Union[Dict[str, Any], requests.Response]]:
@@ -535,8 +518,7 @@ class SaveMixin(_RestObjectBase):
def _get_updated_data(self) -> Dict[str, Any]:
updated_data = {}
- required, optional = self.manager.get_update_attrs()
- for attr in required:
+ for attr in self.manager._update_attrs[0]:
# Get everything required, no matter if it's been updated
updated_data[attr] = getattr(self, attr)
# Add the updated attributes