Skip to content

Commit

Permalink
chore: have _create_attrs & _update_attrs be a namedtuple
Browse files Browse the repository at this point in the history
Convert _create_attrs and _update_attrs to use a NamedTuple
(RequiredOptional) to help with code readability. Update all code to
use the NamedTuple.
  • Loading branch information
JohnVillalovos authored and nejch committed Apr 17, 2021
1 parent 8603248 commit aee1f49
Show file tree
Hide file tree
Showing 45 changed files with 362 additions and 286 deletions.
12 changes: 9 additions & 3 deletions gitlab/base.py
Expand Up @@ -17,12 +17,13 @@

import importlib
from types import ModuleType
from typing import Any, Dict, Optional, Tuple, Type
from typing import Any, Dict, NamedTuple, Optional, Tuple, Type

from .client import Gitlab, GitlabList
from gitlab import types as g_types

__all__ = [
"RequiredOptional",
"RESTObject",
"RESTObjectList",
"RESTManager",
Expand Down Expand Up @@ -249,6 +250,11 @@ def total(self) -> int:
return self._list.total


class RequiredOptional(NamedTuple):
required: Tuple[str, ...] = tuple()
optional: Tuple[str, ...] = tuple()


class RESTManager(object):
"""Base class for CRUD operations on objects.
Expand All @@ -258,8 +264,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())
_create_attrs: RequiredOptional = RequiredOptional()
_update_attrs: RequiredOptional = RequiredOptional()
_path: Optional[str] = None
_obj_cls: Optional[Type[RESTObject]] = None
_from_parent_attrs: Dict[str, Any] = {}
Expand Down
6 changes: 3 additions & 3 deletions gitlab/mixins.py
Expand Up @@ -267,7 +267,7 @@ class CreateMixin(_RestManagerBase):

def _check_missing_create_attrs(self, data: Dict[str, Any]) -> None:
missing = []
for attr in self._create_attrs[0]:
for attr in self._create_attrs.required:
if attr not in data:
missing.append(attr)
continue
Expand Down Expand Up @@ -339,7 +339,7 @@ def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None:
# 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]
[k for k in self._update_attrs.required if k != self._obj_cls._id_attr]
)
missing = []
for attr in required:
Expand Down Expand Up @@ -518,7 +518,7 @@ class SaveMixin(_RestObjectBase):

def _get_updated_data(self) -> Dict[str, Any]:
updated_data = {}
for attr in self.manager._update_attrs[0]:
for attr in self.manager._update_attrs.required:
# Get everything required, no matter if it's been updated
updated_data[attr] = getattr(self, attr)
# Add the updated attributes
Expand Down
32 changes: 22 additions & 10 deletions gitlab/tests/mixins/test_mixin_methods.py
Expand Up @@ -131,7 +131,9 @@ def resp_cont(url, request):

def test_create_mixin_missing_attrs(gl):
class M(CreateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
_create_attrs = base.RequiredOptional(
required=("foo",), optional=("bar", "baz")
)

mgr = M(gl)
data = {"foo": "bar", "baz": "blah"}
Expand All @@ -145,8 +147,10 @@ class M(CreateMixin, FakeManager):

def test_create_mixin(gl):
class M(CreateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
_update_attrs = (("foo",), ("bam",))
_create_attrs = base.RequiredOptional(
required=("foo",), optional=("bar", "baz")
)
_update_attrs = base.RequiredOptional(required=("foo",), optional=("bam",))

@urlmatch(scheme="http", netloc="localhost", path="/api/v4/tests", method="post")
def resp_cont(url, request):
Expand All @@ -164,8 +168,10 @@ def resp_cont(url, request):

def test_create_mixin_custom_path(gl):
class M(CreateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
_update_attrs = (("foo",), ("bam",))
_create_attrs = base.RequiredOptional(
required=("foo",), optional=("bar", "baz")
)
_update_attrs = base.RequiredOptional(required=("foo",), optional=("bam",))

@urlmatch(scheme="http", netloc="localhost", path="/api/v4/others", method="post")
def resp_cont(url, request):
Expand All @@ -183,7 +189,9 @@ def resp_cont(url, request):

def test_update_mixin_missing_attrs(gl):
class M(UpdateMixin, FakeManager):
_update_attrs = (("foo",), ("bar", "baz"))
_update_attrs = base.RequiredOptional(
required=("foo",), optional=("bar", "baz")
)

mgr = M(gl)
data = {"foo": "bar", "baz": "blah"}
Expand All @@ -197,8 +205,10 @@ class M(UpdateMixin, FakeManager):

def test_update_mixin(gl):
class M(UpdateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
_update_attrs = (("foo",), ("bam",))
_create_attrs = base.RequiredOptional(
required=("foo",), optional=("bar", "baz")
)
_update_attrs = base.RequiredOptional(required=("foo",), optional=("bam",))

@urlmatch(scheme="http", netloc="localhost", path="/api/v4/tests/42", method="put")
def resp_cont(url, request):
Expand All @@ -216,8 +226,10 @@ def resp_cont(url, request):

def test_update_mixin_no_id(gl):
class M(UpdateMixin, FakeManager):
_create_attrs = (("foo",), ("bar", "baz"))
_update_attrs = (("foo",), ("bam",))
_create_attrs = base.RequiredOptional(
required=("foo",), optional=("bar", "baz")
)
_update_attrs = base.RequiredOptional(required=("foo",), optional=("bam",))

@urlmatch(scheme="http", netloc="localhost", path="/api/v4/tests", method="put")
def resp_cont(url, request):
Expand Down
8 changes: 4 additions & 4 deletions gitlab/v4/cli.py
Expand Up @@ -177,11 +177,11 @@ def _populate_sub_parser_by_class(cls, sub_parser):
]

if action_name == "create":
for x in mgr_cls._create_attrs[0]:
for x in mgr_cls._create_attrs.required:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=True
)
for x in mgr_cls._create_attrs[1]:
for x in mgr_cls._create_attrs.optional:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=False
)
Expand All @@ -191,13 +191,13 @@ def _populate_sub_parser_by_class(cls, sub_parser):
id_attr = cls._id_attr.replace("_", "-")
sub_parser_action.add_argument("--%s" % id_attr, required=True)

for x in mgr_cls._update_attrs[0]:
for x in mgr_cls._update_attrs.required:
if x != cls._id_attr:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=True
)

for x in mgr_cls._update_attrs[1]:
for x in mgr_cls._update_attrs.optional:
if x != cls._id_attr:
sub_parser_action.add_argument(
"--%s" % x.replace("_", "-"), required=False
Expand Down
7 changes: 3 additions & 4 deletions gitlab/v4/objects/appearance.py
@@ -1,5 +1,5 @@
from gitlab import exceptions as exc
from gitlab.base import RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.mixins import GetWithoutIdMixin, SaveMixin, UpdateMixin


Expand All @@ -16,9 +16,8 @@ class ApplicationAppearance(SaveMixin, RESTObject):
class ApplicationAppearanceManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
_path = "/application/appearance"
_obj_cls = ApplicationAppearance
_update_attrs = (
tuple(),
(
_update_attrs = RequiredOptional(
optional=(
"title",
"description",
"logo",
Expand Down
6 changes: 4 additions & 2 deletions gitlab/v4/objects/applications.py
@@ -1,4 +1,4 @@
from gitlab.base import RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.mixins import CreateMixin, DeleteMixin, ListMixin, ObjectDeleteMixin

__all__ = [
Expand All @@ -15,4 +15,6 @@ class Application(ObjectDeleteMixin, RESTObject):
class ApplicationManager(ListMixin, CreateMixin, DeleteMixin, RESTManager):
_path = "/applications"
_obj_cls = Application
_create_attrs = (("name", "redirect_uri", "scopes"), ("confidential",))
_create_attrs = RequiredOptional(
required=("name", "redirect_uri", "scopes"), optional=("confidential",)
)
14 changes: 7 additions & 7 deletions gitlab/v4/objects/award_emojis.py
@@ -1,4 +1,4 @@
from gitlab.base import RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.mixins import NoUpdateMixin, ObjectDeleteMixin


Expand Down Expand Up @@ -26,7 +26,7 @@ class ProjectIssueAwardEmojiManager(NoUpdateMixin, RESTManager):
_path = "/projects/%(project_id)s/issues/%(issue_iid)s/award_emoji"
_obj_cls = ProjectIssueAwardEmoji
_from_parent_attrs = {"project_id": "project_id", "issue_iid": "iid"}
_create_attrs = (("name",), tuple())
_create_attrs = RequiredOptional(required=("name",))


class ProjectIssueNoteAwardEmoji(ObjectDeleteMixin, RESTObject):
Expand All @@ -43,7 +43,7 @@ class ProjectIssueNoteAwardEmojiManager(NoUpdateMixin, RESTManager):
"issue_iid": "issue_iid",
"note_id": "id",
}
_create_attrs = (("name",), tuple())
_create_attrs = RequiredOptional(required=("name",))


class ProjectMergeRequestAwardEmoji(ObjectDeleteMixin, RESTObject):
Expand All @@ -54,7 +54,7 @@ class ProjectMergeRequestAwardEmojiManager(NoUpdateMixin, RESTManager):
_path = "/projects/%(project_id)s/merge_requests/%(mr_iid)s/award_emoji"
_obj_cls = ProjectMergeRequestAwardEmoji
_from_parent_attrs = {"project_id": "project_id", "mr_iid": "iid"}
_create_attrs = (("name",), tuple())
_create_attrs = RequiredOptional(required=("name",))


class ProjectMergeRequestNoteAwardEmoji(ObjectDeleteMixin, RESTObject):
Expand All @@ -72,7 +72,7 @@ class ProjectMergeRequestNoteAwardEmojiManager(NoUpdateMixin, RESTManager):
"mr_iid": "mr_iid",
"note_id": "id",
}
_create_attrs = (("name",), tuple())
_create_attrs = RequiredOptional(required=("name",))


class ProjectSnippetAwardEmoji(ObjectDeleteMixin, RESTObject):
Expand All @@ -83,7 +83,7 @@ class ProjectSnippetAwardEmojiManager(NoUpdateMixin, RESTManager):
_path = "/projects/%(project_id)s/snippets/%(snippet_id)s/award_emoji"
_obj_cls = ProjectSnippetAwardEmoji
_from_parent_attrs = {"project_id": "project_id", "snippet_id": "id"}
_create_attrs = (("name",), tuple())
_create_attrs = RequiredOptional(required=("name",))


class ProjectSnippetNoteAwardEmoji(ObjectDeleteMixin, RESTObject):
Expand All @@ -101,4 +101,4 @@ class ProjectSnippetNoteAwardEmojiManager(NoUpdateMixin, RESTManager):
"snippet_id": "snippet_id",
"note_id": "id",
}
_create_attrs = (("name",), tuple())
_create_attrs = RequiredOptional(required=("name",))
10 changes: 5 additions & 5 deletions gitlab/v4/objects/badges.py
@@ -1,4 +1,4 @@
from gitlab.base import RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.mixins import BadgeRenderMixin, CRUDMixin, ObjectDeleteMixin, SaveMixin


Expand All @@ -18,8 +18,8 @@ class GroupBadgeManager(BadgeRenderMixin, CRUDMixin, RESTManager):
_path = "/groups/%(group_id)s/badges"
_obj_cls = GroupBadge
_from_parent_attrs = {"group_id": "id"}
_create_attrs = (("link_url", "image_url"), tuple())
_update_attrs = (tuple(), ("link_url", "image_url"))
_create_attrs = RequiredOptional(required=("link_url", "image_url"))
_update_attrs = RequiredOptional(optional=("link_url", "image_url"))


class ProjectBadge(SaveMixin, ObjectDeleteMixin, RESTObject):
Expand All @@ -30,5 +30,5 @@ class ProjectBadgeManager(BadgeRenderMixin, CRUDMixin, RESTManager):
_path = "/projects/%(project_id)s/badges"
_obj_cls = ProjectBadge
_from_parent_attrs = {"project_id": "id"}
_create_attrs = (("link_url", "image_url"), tuple())
_update_attrs = (tuple(), ("link_url", "image_url"))
_create_attrs = RequiredOptional(required=("link_url", "image_url"))
_update_attrs = RequiredOptional(optional=("link_url", "image_url"))
14 changes: 7 additions & 7 deletions gitlab/v4/objects/boards.py
@@ -1,4 +1,4 @@
from gitlab.base import RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.mixins import CRUDMixin, ObjectDeleteMixin, SaveMixin


Expand All @@ -22,8 +22,8 @@ class GroupBoardListManager(CRUDMixin, RESTManager):
_path = "/groups/%(group_id)s/boards/%(board_id)s/lists"
_obj_cls = GroupBoardList
_from_parent_attrs = {"group_id": "group_id", "board_id": "id"}
_create_attrs = (("label_id",), tuple())
_update_attrs = (("position",), tuple())
_create_attrs = RequiredOptional(required=("label_id",))
_update_attrs = RequiredOptional(required=("position",))


class GroupBoard(SaveMixin, ObjectDeleteMixin, RESTObject):
Expand All @@ -34,7 +34,7 @@ class GroupBoardManager(CRUDMixin, RESTManager):
_path = "/groups/%(group_id)s/boards"
_obj_cls = GroupBoard
_from_parent_attrs = {"group_id": "id"}
_create_attrs = (("name",), tuple())
_create_attrs = RequiredOptional(required=("name",))


class ProjectBoardList(SaveMixin, ObjectDeleteMixin, RESTObject):
Expand All @@ -45,8 +45,8 @@ class ProjectBoardListManager(CRUDMixin, RESTManager):
_path = "/projects/%(project_id)s/boards/%(board_id)s/lists"
_obj_cls = ProjectBoardList
_from_parent_attrs = {"project_id": "project_id", "board_id": "id"}
_create_attrs = (("label_id",), tuple())
_update_attrs = (("position",), tuple())
_create_attrs = RequiredOptional(required=("label_id",))
_update_attrs = RequiredOptional(required=("position",))


class ProjectBoard(SaveMixin, ObjectDeleteMixin, RESTObject):
Expand All @@ -57,4 +57,4 @@ class ProjectBoardManager(CRUDMixin, RESTManager):
_path = "/projects/%(project_id)s/boards"
_obj_cls = ProjectBoard
_from_parent_attrs = {"project_id": "id"}
_create_attrs = (("name",), tuple())
_create_attrs = RequiredOptional(required=("name",))
10 changes: 5 additions & 5 deletions gitlab/v4/objects/branches.py
@@ -1,6 +1,6 @@
from gitlab import cli
from gitlab import exceptions as exc
from gitlab.base import RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.mixins import NoUpdateMixin, ObjectDeleteMixin


Expand Down Expand Up @@ -64,7 +64,7 @@ class ProjectBranchManager(NoUpdateMixin, RESTManager):
_path = "/projects/%(project_id)s/repository/branches"
_obj_cls = ProjectBranch
_from_parent_attrs = {"project_id": "id"}
_create_attrs = (("branch", "ref"), tuple())
_create_attrs = RequiredOptional(required=("branch", "ref"))


class ProjectProtectedBranch(ObjectDeleteMixin, RESTObject):
Expand All @@ -75,9 +75,9 @@ class ProjectProtectedBranchManager(NoUpdateMixin, RESTManager):
_path = "/projects/%(project_id)s/protected_branches"
_obj_cls = ProjectProtectedBranch
_from_parent_attrs = {"project_id": "id"}
_create_attrs = (
("name",),
(
_create_attrs = RequiredOptional(
required=("name",),
optional=(
"push_access_level",
"merge_access_level",
"unprotect_access_level",
Expand Down
10 changes: 7 additions & 3 deletions gitlab/v4/objects/broadcast_messages.py
@@ -1,4 +1,4 @@
from gitlab.base import RESTManager, RESTObject
from gitlab.base import RequiredOptional, RESTManager, RESTObject
from gitlab.mixins import CRUDMixin, ObjectDeleteMixin, SaveMixin


Expand All @@ -16,5 +16,9 @@ class BroadcastMessageManager(CRUDMixin, RESTManager):
_path = "/broadcast_messages"
_obj_cls = BroadcastMessage

_create_attrs = (("message",), ("starts_at", "ends_at", "color", "font"))
_update_attrs = (tuple(), ("message", "starts_at", "ends_at", "color", "font"))
_create_attrs = RequiredOptional(
required=("message",), optional=("starts_at", "ends_at", "color", "font")
)
_update_attrs = RequiredOptional(
optional=("message", "starts_at", "ends_at", "color", "font")
)

0 comments on commit aee1f49

Please sign in to comment.