Skip to content

chore: make _types always present in RESTManager #1365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gitlab/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Any, Dict, Optional, Type

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

__all__ = [
"RESTObject",
Expand Down Expand Up @@ -260,6 +261,7 @@ class RESTManager(object):
_path: Optional[str] = None
_obj_cls: Optional[Type[RESTObject]] = None
_from_parent_attrs: Dict[str, Any] = {}
_types: Dict[str, Type[g_types.GitlabAttribute]] = {}

_computed_path: Optional[str]
_parent: Optional[RESTObject]
Expand Down
19 changes: 8 additions & 11 deletions gitlab/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,8 @@ def list(self, **kwargs: Any) -> Union[base.RESTObjectList, List[base.RESTObject
data.setdefault("order_by", self.gitlab.order_by)

# We get the attributes that need some special transformation
types = getattr(self, "_types", {})
if types:
for attr_name, type_cls in types.items():
if self._types:
for attr_name, type_cls in self._types.items():
if attr_name in data.keys():
type_obj = type_cls(data[attr_name])
data[attr_name] = type_obj.get_for_api()
Expand Down Expand Up @@ -311,17 +310,16 @@ def create(
files = {}

# We get the attributes that need some special transformation
types = getattr(self, "_types", {})
if types:
if self._types:
# Duplicate data to avoid messing with what the user sent us
data = data.copy()
for attr_name, type_cls in types.items():
for attr_name, type_cls in self._types.items():
if attr_name in data.keys():
type_obj = type_cls(data[attr_name])

# if the type if FileAttribute we need to pass the data as
# file
if issubclass(type_cls, g_types.FileAttribute):
if isinstance(type_obj, g_types.FileAttribute):
k = type_obj.get_file_name(attr_name)
files[attr_name] = (k, data.pop(attr_name))
else:
Expand Down Expand Up @@ -414,17 +412,16 @@ def update(
files = {}

# We get the attributes that need some special transformation
types = getattr(self, "_types", {})
if types:
if self._types:
# Duplicate data to avoid messing with what the user sent us
new_data = new_data.copy()
for attr_name, type_cls in types.items():
for attr_name, type_cls in self._types.items():
if attr_name in new_data.keys():
type_obj = type_cls(new_data[attr_name])

# if the type if FileAttribute we need to pass the data as
# file
if issubclass(type_cls, g_types.FileAttribute):
if isinstance(type_obj, g_types.FileAttribute):
k = type_obj.get_file_name(attr_name)
files[attr_name] = (k, new_data.pop(attr_name))
else:
Expand Down
5 changes: 2 additions & 3 deletions gitlab/v4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def __init__(self, gl, what, action, args):
self.mgr_cls._path = self.mgr_cls._path % self.args
self.mgr = self.mgr_cls(gl)

types = getattr(self.mgr_cls, "_types", {})
if types:
for attr_name, type_cls in types.items():
if self.mgr_cls._types:
for attr_name, type_cls in self.mgr_cls._types.items():
if attr_name in self.args.keys():
obj = type_cls()
obj.set_from_cli(self.args[attr_name])
Expand Down