summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gitlab/base.py4
-rw-r--r--gitlab/mixins.py32
-rw-r--r--gitlab/tests/mixins/test_mixin_methods.py42
-rw-r--r--gitlab/v4/cli.py35
4 files changed, 22 insertions, 91 deletions
diff --git a/gitlab/base.py b/gitlab/base.py
index 62ace95..5eb1118 100644
--- a/gitlab/base.py
+++ b/gitlab/base.py
@@ -17,7 +17,7 @@
import importlib
from types import ModuleType
-from typing import Any, Dict, Optional, Type
+from typing import Any, Dict, Optional, Tuple, Type
from .client import Gitlab, GitlabList
from gitlab import types as g_types
@@ -258,6 +258,8 @@ class RESTManager(object):
``_obj_cls``: The class of objects that will be created
"""
+ _create_attrs: Tuple[Tuple[str, ...], Tuple[str, ...]] = (tuple(), tuple())
+ _update_attrs: Tuple[Tuple[str, ...], Tuple[str, ...]] = (tuple(), tuple())
_path: Optional[str] = None
_obj_cls: Optional[Type[RESTObject]] = None
_from_parent_attrs: Dict[str, Any] = {}
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
diff --git a/gitlab/tests/mixins/test_mixin_methods.py b/gitlab/tests/mixins/test_mixin_methods.py
index 171e90c..1dafa74 100644
--- a/gitlab/tests/mixins/test_mixin_methods.py
+++ b/gitlab/tests/mixins/test_mixin_methods.py
@@ -129,27 +129,6 @@ def test_list_other_url(gl):
obj_list.next()
-def test_create_mixin_get_attrs(gl):
- class M1(CreateMixin, FakeManager):
- pass
-
- class M2(CreateMixin, FakeManager):
- _create_attrs = (("foo",), ("bar", "baz"))
- _update_attrs = (("foo",), ("bam",))
-
- mgr = M1(gl)
- required, optional = mgr.get_create_attrs()
- assert len(required) == 0
- assert len(optional) == 0
-
- mgr = M2(gl)
- required, optional = mgr.get_create_attrs()
- assert "foo" in required
- assert "bar" in optional
- assert "baz" in optional
- assert "bam" not in optional
-
-
def test_create_mixin_missing_attrs(gl):
class M(CreateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
@@ -202,27 +181,6 @@ def test_create_mixin_custom_path(gl):
assert obj.foo == "bar"
-def test_update_mixin_get_attrs(gl):
- class M1(UpdateMixin, FakeManager):
- pass
-
- class M2(UpdateMixin, FakeManager):
- _create_attrs = (("foo",), ("bar", "baz"))
- _update_attrs = (("foo",), ("bam",))
-
- mgr = M1(gl)
- required, optional = mgr.get_update_attrs()
- assert len(required) == 0
- assert len(optional) == 0
-
- mgr = M2(gl)
- required, optional = mgr.get_update_attrs()
- assert "foo" in required
- assert "bam" in optional
- assert "bar" not in optional
- assert "baz" not in optional
-
-
def test_update_mixin_missing_attrs(gl):
class M(UpdateMixin, FakeManager):
_update_attrs = (("foo",), ("bar", "baz"))
diff --git a/gitlab/v4/cli.py b/gitlab/v4/cli.py
index c01f06b..df645bf 100644
--- a/gitlab/v4/cli.py
+++ b/gitlab/v4/cli.py
@@ -177,42 +177,31 @@ def _populate_sub_parser_by_class(cls, sub_parser):
]
if action_name == "create":
- if hasattr(mgr_cls, "_create_attrs"):
- [
- sub_parser_action.add_argument(
- "--%s" % x.replace("_", "-"), required=True
- )
- for x in mgr_cls._create_attrs[0]
- ]
-
- [
- sub_parser_action.add_argument(
- "--%s" % x.replace("_", "-"), required=False
- )
- for x in mgr_cls._create_attrs[1]
- ]
+ for x in mgr_cls._create_attrs[0]:
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=True
+ )
+ for x in mgr_cls._create_attrs[1]:
+ sub_parser_action.add_argument(
+ "--%s" % x.replace("_", "-"), required=False
+ )
if action_name == "update":
if cls._id_attr is not None:
id_attr = cls._id_attr.replace("_", "-")
sub_parser_action.add_argument("--%s" % id_attr, required=True)
- if hasattr(mgr_cls, "_update_attrs"):
- [
+ for x in mgr_cls._update_attrs[0]:
+ if x != cls._id_attr:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=True
)
- for x in mgr_cls._update_attrs[0]
- if x != cls._id_attr
- ]
- [
+ for x in mgr_cls._update_attrs[1]:
+ if x != cls._id_attr:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=False
)
- for x in mgr_cls._update_attrs[1]
- if x != cls._id_attr
- ]
if cls.__name__ in cli.custom_actions:
name = cls.__name__