Skip to content

Commit

Permalink
chore: add _create_attrs & _update_attrs to RESTManager
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohnVillalovos committed Mar 10, 2021
1 parent 6fde243 commit 147f05d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 91 deletions.
4 changes: 3 additions & 1 deletion gitlab/base.py
Expand Up @@ -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
Expand Down Expand Up @@ -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] = {}
Expand Down
32 changes: 7 additions & 25 deletions gitlab/mixins.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -357,15 +349,6 @@ def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None:
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]]:
Expand Down Expand Up @@ -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
Expand Down
42 changes: 0 additions & 42 deletions gitlab/tests/mixins/test_mixin_methods.py
Expand Up @@ -129,27 +129,6 @@ def resp_cont(url, request):
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"))
Expand Down Expand Up @@ -202,27 +181,6 @@ def resp_cont(url, request):
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"))
Expand Down
35 changes: 12 additions & 23 deletions gitlab/v4/cli.py
Expand Up @@ -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__
Expand Down

0 comments on commit 147f05d

Please sign in to comment.