Skip to content

Commit d8de4dc

Browse files
chore: convert to using type-annotations for managers
Convert our manager usage to be done via type annotations. Now to define a manager to be used in a RESTObject subclass can simply do: class ExampleClass(CRUDMixin, RESTObject): my_manager: MyManager Any type-annotation that annotates it to be of type *Manager (with the exception of RESTManager) will cause the manager to be created on the object.
1 parent c9b5d3b commit d8de4dc

21 files changed

+15
-242
lines changed

gitlab/base.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class RESTObject(object):
4949
_parent_attrs: Dict[str, Any]
5050
_short_print_attr: Optional[str] = None
5151
_updated_attrs: Dict[str, Any]
52-
_managers: Optional[Iterable[Tuple[str, str]]] = None
5352
manager: "RESTManager"
5453

5554
def __init__(self, manager: "RESTManager", attrs: Dict[str, Any]) -> None:
@@ -151,10 +150,19 @@ def __hash__(self) -> int:
151150
return hash(self.get_id())
152151

153152
def _create_managers(self) -> None:
154-
if self._managers is None:
155-
return
156-
157-
for attr, cls_name in self._managers:
153+
# NOTE(jlvillal): We are creating our managers by looking at the class
154+
# annotations. If an attribute is annotated as being a *Manager type
155+
# then we create the manager and assign it to the attribute.
156+
for attr, annotation in sorted(self.__annotations__.items()):
157+
if not isinstance(annotation, (type, str)):
158+
continue
159+
if isinstance(annotation, type):
160+
cls_name = annotation.__name__
161+
else:
162+
cls_name = annotation
163+
# All *Manager classes are used except for the base "RESTManager" class
164+
if cls_name == "RESTManager" or not cls_name.endswith("Manager"):
165+
continue
158166
cls = getattr(self._module, cls_name)
159167
manager = cls(self.manager.gitlab, parent=self)
160168
# Since we have our own __setattr__ method, we can't use setattr()

gitlab/v4/objects/boards.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class GroupBoardListManager(CRUDMixin, RESTManager):
2727

2828
class GroupBoard(SaveMixin, ObjectDeleteMixin, RESTObject):
2929
lists: GroupBoardListManager
30-
_managers = (("lists", "GroupBoardListManager"),)
3130

3231

3332
class GroupBoardManager(CRUDMixin, RESTManager):
@@ -51,7 +50,6 @@ class ProjectBoardListManager(CRUDMixin, RESTManager):
5150

5251
class ProjectBoard(SaveMixin, ObjectDeleteMixin, RESTObject):
5352
lists: ProjectBoardListManager
54-
_managers = (("lists", "ProjectBoardListManager"),)
5553

5654

5755
class ProjectBoardManager(CRUDMixin, RESTManager):

gitlab/v4/objects/commits.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ class ProjectCommit(RESTObject):
2121
comments: "ProjectCommitCommentManager"
2222
discussions: ProjectCommitDiscussionManager
2323
statuses: "ProjectCommitStatusManager"
24-
_managers = (
25-
("comments", "ProjectCommitCommentManager"),
26-
("discussions", "ProjectCommitDiscussionManager"),
27-
("statuses", "ProjectCommitStatusManager"),
28-
)
2924

3025
@cli.register_custom_action("ProjectCommit")
3126
@exc.on_http_error(exc.GitlabGetError)

gitlab/v4/objects/container_registry.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
class ProjectRegistryRepository(ObjectDeleteMixin, RESTObject):
1515
tags: "ProjectRegistryTagManager"
16-
_managers = (("tags", "ProjectRegistryTagManager"),)
1716

1817

1918
class ProjectRegistryRepositoryManager(DeleteMixin, ListMixin, RESTManager):

gitlab/v4/objects/deployments.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
class ProjectDeployment(SaveMixin, RESTObject):
1313
mergerequests: ProjectDeploymentMergeRequestManager
14-
_managers = (("mergerequests", "ProjectDeploymentMergeRequestManager"),)
1514

1615

1716
class ProjectDeploymentManager(RetrieveMixin, CreateMixin, UpdateMixin, RESTManager):

gitlab/v4/objects/discussions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
class ProjectCommitDiscussion(RESTObject):
2424
notes: ProjectCommitDiscussionNoteManager
25-
_managers = (("notes", "ProjectCommitDiscussionNoteManager"),)
2625

2726

2827
class ProjectCommitDiscussionManager(RetrieveMixin, CreateMixin, RESTManager):
@@ -34,7 +33,6 @@ class ProjectCommitDiscussionManager(RetrieveMixin, CreateMixin, RESTManager):
3433

3534
class ProjectIssueDiscussion(RESTObject):
3635
notes: ProjectIssueDiscussionNoteManager
37-
_managers = (("notes", "ProjectIssueDiscussionNoteManager"),)
3836

3937

4038
class ProjectIssueDiscussionManager(RetrieveMixin, CreateMixin, RESTManager):
@@ -46,7 +44,6 @@ class ProjectIssueDiscussionManager(RetrieveMixin, CreateMixin, RESTManager):
4644

4745
class ProjectMergeRequestDiscussion(SaveMixin, RESTObject):
4846
notes: ProjectMergeRequestDiscussionNoteManager
49-
_managers = (("notes", "ProjectMergeRequestDiscussionNoteManager"),)
5047

5148

5249
class ProjectMergeRequestDiscussionManager(
@@ -63,7 +60,6 @@ class ProjectMergeRequestDiscussionManager(
6360

6461
class ProjectSnippetDiscussion(RESTObject):
6562
notes: ProjectSnippetDiscussionNoteManager
66-
_managers = (("notes", "ProjectSnippetDiscussionNoteManager"),)
6763

6864

6965
class ProjectSnippetDiscussionManager(RetrieveMixin, CreateMixin, RESTManager):

gitlab/v4/objects/epics.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class GroupEpic(ObjectDeleteMixin, SaveMixin, RESTObject):
2626

2727
issues: "GroupEpicIssueManager"
2828
resourcelabelevents: GroupEpicResourceLabelEventManager
29-
_managers = (
30-
("issues", "GroupEpicIssueManager"),
31-
("resourcelabelevents", "GroupEpicResourceLabelEventManager"),
32-
)
3329

3430

3531
class GroupEpicManager(CRUDMixin, RESTManager):

gitlab/v4/objects/groups.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,35 +71,6 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
7171
subgroups: "GroupSubgroupManager"
7272
variables: GroupVariableManager
7373
wikis: GroupWikiManager
74-
_managers = (
75-
("accessrequests", "GroupAccessRequestManager"),
76-
("audit_events", "GroupAuditEventManager"),
77-
("badges", "GroupBadgeManager"),
78-
("billable_members", "GroupBillableMemberManager"),
79-
("boards", "GroupBoardManager"),
80-
("customattributes", "GroupCustomAttributeManager"),
81-
("descendant_groups", "GroupDescendantGroupManager"),
82-
("exports", "GroupExportManager"),
83-
("epics", "GroupEpicManager"),
84-
("hooks", "GroupHookManager"),
85-
("imports", "GroupImportManager"),
86-
("issues", "GroupIssueManager"),
87-
("issues_statistics", "GroupIssuesStatisticsManager"),
88-
("labels", "GroupLabelManager"),
89-
("members", "GroupMemberManager"),
90-
("members_all", "GroupMemberAllManager"),
91-
("mergerequests", "GroupMergeRequestManager"),
92-
("milestones", "GroupMilestoneManager"),
93-
("notificationsettings", "GroupNotificationSettingsManager"),
94-
("packages", "GroupPackageManager"),
95-
("projects", "GroupProjectManager"),
96-
("runners", "GroupRunnerManager"),
97-
("subgroups", "GroupSubgroupManager"),
98-
("variables", "GroupVariableManager"),
99-
("clusters", "GroupClusterManager"),
100-
("deploytokens", "GroupDeployTokenManager"),
101-
("wikis", "GroupWikiManager"),
102-
)
10374

10475
@cli.register_custom_action("Group", ("to_project_id",))
10576
@exc.on_http_error(exc.GitlabTransferProjectError)

gitlab/v4/objects/issues.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,6 @@ class ProjectIssue(
113113
resourcelabelevents: ProjectIssueResourceLabelEventManager
114114
resourcemilestoneevents: ProjectIssueResourceMilestoneEventManager
115115
resourcestateevents: ProjectIssueResourceStateEventManager
116-
_managers = (
117-
("awardemojis", "ProjectIssueAwardEmojiManager"),
118-
("discussions", "ProjectIssueDiscussionManager"),
119-
("links", "ProjectIssueLinkManager"),
120-
("notes", "ProjectIssueNoteManager"),
121-
("resourcelabelevents", "ProjectIssueResourceLabelEventManager"),
122-
("resourcemilestoneevents", "ProjectIssueResourceMilestoneEventManager"),
123-
("resourcestateevents", "ProjectIssueResourceStateEventManager"),
124-
)
125116

126117
@cli.register_custom_action("ProjectIssue", ("to_project_id",))
127118
@exc.on_http_error(exc.GitlabUpdateError)

gitlab/v4/objects/members.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class GroupBillableMember(ObjectDeleteMixin, RESTObject):
4545
_short_print_attr = "username"
4646

4747
memberships: "GroupBillableMemberMembershipManager"
48-
_managers = (("memberships", "GroupBillableMemberMembershipManager"),)
4948

5049

5150
class GroupBillableMemberManager(ListMixin, DeleteMixin, RESTManager):

0 commit comments

Comments
 (0)