summaryrefslogtreecommitdiff
path: root/gitlab/base.py
diff options
context:
space:
mode:
authorJohn L. Villalovos <john@sodarock.com>2021-06-13 14:40:46 -0700
committerJohn L. Villalovos <john@sodarock.com>2021-09-08 07:18:48 -0700
commitd8de4dc373dc608be6cf6ba14a2acc7efd3fa7a7 (patch)
treed587e4cb383b136cd9947377e43bd52900121994 /gitlab/base.py
parentc9b5d3bac8f7c1f779dd57653f718dd0fac4db4b (diff)
downloadgitlab-d8de4dc373dc608be6cf6ba14a2acc7efd3fa7a7.tar.gz
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.
Diffstat (limited to 'gitlab/base.py')
-rw-r--r--gitlab/base.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/gitlab/base.py b/gitlab/base.py
index 361832e..bc96e0f 100644
--- a/gitlab/base.py
+++ b/gitlab/base.py
@@ -49,7 +49,6 @@ class RESTObject(object):
_parent_attrs: Dict[str, Any]
_short_print_attr: Optional[str] = None
_updated_attrs: Dict[str, Any]
- _managers: Optional[Iterable[Tuple[str, str]]] = None
manager: "RESTManager"
def __init__(self, manager: "RESTManager", attrs: Dict[str, Any]) -> None:
@@ -151,10 +150,19 @@ class RESTObject(object):
return hash(self.get_id())
def _create_managers(self) -> None:
- if self._managers is None:
- return
-
- for attr, cls_name in self._managers:
+ # NOTE(jlvillal): We are creating our managers by looking at the class
+ # annotations. If an attribute is annotated as being a *Manager type
+ # then we create the manager and assign it to the attribute.
+ for attr, annotation in sorted(self.__annotations__.items()):
+ if not isinstance(annotation, (type, str)):
+ continue
+ if isinstance(annotation, type):
+ cls_name = annotation.__name__
+ else:
+ cls_name = annotation
+ # All *Manager classes are used except for the base "RESTManager" class
+ if cls_name == "RESTManager" or not cls_name.endswith("Manager"):
+ continue
cls = getattr(self._module, cls_name)
manager = cls(self.manager.gitlab, parent=self)
# Since we have our own __setattr__ method, we can't use setattr()