Skip to content

Commit 147f05d

Browse files
chore: add _create_attrs & _update_attrs to RESTManager
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.
1 parent 6fde243 commit 147f05d

File tree

4 files changed

+22
-91
lines changed

4 files changed

+22
-91
lines changed

gitlab/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import importlib
1919
from types import ModuleType
20-
from typing import Any, Dict, Optional, Type
20+
from typing import Any, Dict, Optional, Tuple, Type
2121

2222
from .client import Gitlab, GitlabList
2323
from gitlab import types as g_types
@@ -258,6 +258,8 @@ class RESTManager(object):
258258
``_obj_cls``: The class of objects that will be created
259259
"""
260260

261+
_create_attrs: Tuple[Tuple[str, ...], Tuple[str, ...]] = (tuple(), tuple())
262+
_update_attrs: Tuple[Tuple[str, ...], Tuple[str, ...]] = (tuple(), tuple())
261263
_path: Optional[str] = None
262264
_obj_cls: Optional[Type[RESTObject]] = None
263265
_from_parent_attrs: Dict[str, Any] = {}

gitlab/mixins.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,14 @@ class CreateMixin(_RestManagerBase):
266266
gitlab: gitlab.Gitlab
267267

268268
def _check_missing_create_attrs(self, data: Dict[str, Any]) -> None:
269-
required, optional = self.get_create_attrs()
270269
missing = []
271-
for attr in required:
270+
for attr in self._create_attrs[0]:
272271
if attr not in data:
273272
missing.append(attr)
274273
continue
275274
if missing:
276275
raise AttributeError("Missing attributes: %s" % ", ".join(missing))
277276

278-
def get_create_attrs(self) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
279-
"""Return the required and optional arguments.
280-
281-
Returns:
282-
tuple: 2 items: list of required arguments and list of optional
283-
arguments for creation (in that order)
284-
"""
285-
return getattr(self, "_create_attrs", (tuple(), tuple()))
286-
287277
@exc.on_http_error(exc.GitlabCreateError)
288278
def create(
289279
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any
@@ -344,11 +334,13 @@ class UpdateMixin(_RestManagerBase):
344334
gitlab: gitlab.Gitlab
345335

346336
def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None:
347-
required, optional = self.get_update_attrs()
348337
if TYPE_CHECKING:
349338
assert self._obj_cls is not None
350-
# Remove the id field from the required list as it was previously moved to the http path.
351-
required = tuple([k for k in required if k != self._obj_cls._id_attr])
339+
# Remove the id field from the required list as it was previously moved
340+
# to the http path.
341+
required = tuple(
342+
[k for k in self._update_attrs[0] if k != self._obj_cls._id_attr]
343+
)
352344
missing = []
353345
for attr in required:
354346
if attr not in data:
@@ -357,15 +349,6 @@ def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None:
357349
if missing:
358350
raise AttributeError("Missing attributes: %s" % ", ".join(missing))
359351

360-
def get_update_attrs(self) -> Tuple[Tuple[str, ...], Tuple[str, ...]]:
361-
"""Return the required and optional arguments.
362-
363-
Returns:
364-
tuple: 2 items: list of required arguments and list of optional
365-
arguments for update (in that order)
366-
"""
367-
return getattr(self, "_update_attrs", (tuple(), tuple()))
368-
369352
def _get_update_method(
370353
self,
371354
) -> Callable[..., Union[Dict[str, Any], requests.Response]]:
@@ -535,8 +518,7 @@ class SaveMixin(_RestObjectBase):
535518

536519
def _get_updated_data(self) -> Dict[str, Any]:
537520
updated_data = {}
538-
required, optional = self.manager.get_update_attrs()
539-
for attr in required:
521+
for attr in self.manager._update_attrs[0]:
540522
# Get everything required, no matter if it's been updated
541523
updated_data[attr] = getattr(self, attr)
542524
# Add the updated attributes

gitlab/tests/mixins/test_mixin_methods.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,6 @@ def resp_cont(url, request):
129129
obj_list.next()
130130

131131

132-
def test_create_mixin_get_attrs(gl):
133-
class M1(CreateMixin, FakeManager):
134-
pass
135-
136-
class M2(CreateMixin, FakeManager):
137-
_create_attrs = (("foo",), ("bar", "baz"))
138-
_update_attrs = (("foo",), ("bam",))
139-
140-
mgr = M1(gl)
141-
required, optional = mgr.get_create_attrs()
142-
assert len(required) == 0
143-
assert len(optional) == 0
144-
145-
mgr = M2(gl)
146-
required, optional = mgr.get_create_attrs()
147-
assert "foo" in required
148-
assert "bar" in optional
149-
assert "baz" in optional
150-
assert "bam" not in optional
151-
152-
153132
def test_create_mixin_missing_attrs(gl):
154133
class M(CreateMixin, FakeManager):
155134
_create_attrs = (("foo",), ("bar", "baz"))
@@ -202,27 +181,6 @@ def resp_cont(url, request):
202181
assert obj.foo == "bar"
203182

204183

205-
def test_update_mixin_get_attrs(gl):
206-
class M1(UpdateMixin, FakeManager):
207-
pass
208-
209-
class M2(UpdateMixin, FakeManager):
210-
_create_attrs = (("foo",), ("bar", "baz"))
211-
_update_attrs = (("foo",), ("bam",))
212-
213-
mgr = M1(gl)
214-
required, optional = mgr.get_update_attrs()
215-
assert len(required) == 0
216-
assert len(optional) == 0
217-
218-
mgr = M2(gl)
219-
required, optional = mgr.get_update_attrs()
220-
assert "foo" in required
221-
assert "bam" in optional
222-
assert "bar" not in optional
223-
assert "baz" not in optional
224-
225-
226184
def test_update_mixin_missing_attrs(gl):
227185
class M(UpdateMixin, FakeManager):
228186
_update_attrs = (("foo",), ("bar", "baz"))

gitlab/v4/cli.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -177,42 +177,31 @@ def _populate_sub_parser_by_class(cls, sub_parser):
177177
]
178178

179179
if action_name == "create":
180-
if hasattr(mgr_cls, "_create_attrs"):
181-
[
182-
sub_parser_action.add_argument(
183-
"--%s" % x.replace("_", "-"), required=True
184-
)
185-
for x in mgr_cls._create_attrs[0]
186-
]
187-
188-
[
189-
sub_parser_action.add_argument(
190-
"--%s" % x.replace("_", "-"), required=False
191-
)
192-
for x in mgr_cls._create_attrs[1]
193-
]
180+
for x in mgr_cls._create_attrs[0]:
181+
sub_parser_action.add_argument(
182+
"--%s" % x.replace("_", "-"), required=True
183+
)
184+
for x in mgr_cls._create_attrs[1]:
185+
sub_parser_action.add_argument(
186+
"--%s" % x.replace("_", "-"), required=False
187+
)
194188

195189
if action_name == "update":
196190
if cls._id_attr is not None:
197191
id_attr = cls._id_attr.replace("_", "-")
198192
sub_parser_action.add_argument("--%s" % id_attr, required=True)
199193

200-
if hasattr(mgr_cls, "_update_attrs"):
201-
[
194+
for x in mgr_cls._update_attrs[0]:
195+
if x != cls._id_attr:
202196
sub_parser_action.add_argument(
203197
"--%s" % x.replace("_", "-"), required=True
204198
)
205-
for x in mgr_cls._update_attrs[0]
206-
if x != cls._id_attr
207-
]
208199

209-
[
200+
for x in mgr_cls._update_attrs[1]:
201+
if x != cls._id_attr:
210202
sub_parser_action.add_argument(
211203
"--%s" % x.replace("_", "-"), required=False
212204
)
213-
for x in mgr_cls._update_attrs[1]
214-
if x != cls._id_attr
215-
]
216205

217206
if cls.__name__ in cli.custom_actions:
218207
name = cls.__name__

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy