Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit147f05d

Browse files
chore: add _create_attrs & _update_attrs to RESTManager
Add the attributes: _create_attrs and _update_attrs to the RESTManagerclass. 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 parent6fde243 commit147f05d

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
importimportlib
1919
fromtypesimportModuleType
20-
fromtypingimportAny,Dict,Optional,Type
20+
fromtypingimportAny,Dict,Optional,Tuple,Type
2121

2222
from .clientimportGitlab,GitlabList
2323
fromgitlabimporttypesasg_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-
forattrinrequired:
270+
forattrinself._create_attrs[0]:
272271
ifattrnotindata:
273272
missing.append(attr)
274273
continue
275274
ifmissing:
276275
raiseAttributeError("Missing attributes: %s"%", ".join(missing))
277276

278-
defget_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-
returngetattr(self,"_create_attrs", (tuple(),tuple()))
286-
287277
@exc.on_http_error(exc.GitlabCreateError)
288278
defcreate(
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
ifTYPE_CHECKING:
349338
assertself._obj_clsisnotNone
350-
# Remove the id field from the required list as it was previously moved to the http path.
351-
required=tuple([kforkinrequiredifk!=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+
[kforkinself._update_attrs[0]ifk!=self._obj_cls._id_attr]
343+
)
352344
missing= []
353345
forattrinrequired:
354346
ifattrnotindata:
@@ -357,15 +349,6 @@ def _check_missing_update_attrs(self, data: Dict[str, Any]) -> None:
357349
ifmissing:
358350
raiseAttributeError("Missing attributes: %s"%", ".join(missing))
359351

360-
defget_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-
returngetattr(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-
forattrinrequired:
521+
forattrinself.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-
deftest_create_mixin_get_attrs(gl):
133-
classM1(CreateMixin,FakeManager):
134-
pass
135-
136-
classM2(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-
assertlen(required)==0
143-
assertlen(optional)==0
144-
145-
mgr=M2(gl)
146-
required,optional=mgr.get_create_attrs()
147-
assert"foo"inrequired
148-
assert"bar"inoptional
149-
assert"baz"inoptional
150-
assert"bam"notinoptional
151-
152-
153132
deftest_create_mixin_missing_attrs(gl):
154133
classM(CreateMixin,FakeManager):
155134
_create_attrs= (("foo",), ("bar","baz"))
@@ -202,27 +181,6 @@ def resp_cont(url, request):
202181
assertobj.foo=="bar"
203182

204183

205-
deftest_update_mixin_get_attrs(gl):
206-
classM1(UpdateMixin,FakeManager):
207-
pass
208-
209-
classM2(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-
assertlen(required)==0
216-
assertlen(optional)==0
217-
218-
mgr=M2(gl)
219-
required,optional=mgr.get_update_attrs()
220-
assert"foo"inrequired
221-
assert"bam"inoptional
222-
assert"bar"notinoptional
223-
assert"baz"notinoptional
224-
225-
226184
deftest_update_mixin_missing_attrs(gl):
227185
classM(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
ifaction_name=="create":
180-
ifhasattr(mgr_cls,"_create_attrs"):
181-
[
182-
sub_parser_action.add_argument(
183-
"--%s"%x.replace("_","-"),required=True
184-
)
185-
forxinmgr_cls._create_attrs[0]
186-
]
187-
188-
[
189-
sub_parser_action.add_argument(
190-
"--%s"%x.replace("_","-"),required=False
191-
)
192-
forxinmgr_cls._create_attrs[1]
193-
]
180+
forxinmgr_cls._create_attrs[0]:
181+
sub_parser_action.add_argument(
182+
"--%s"%x.replace("_","-"),required=True
183+
)
184+
forxinmgr_cls._create_attrs[1]:
185+
sub_parser_action.add_argument(
186+
"--%s"%x.replace("_","-"),required=False
187+
)
194188

195189
ifaction_name=="update":
196190
ifcls._id_attrisnotNone:
197191
id_attr=cls._id_attr.replace("_","-")
198192
sub_parser_action.add_argument("--%s"%id_attr,required=True)
199193

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

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

217206
ifcls.__name__incli.custom_actions:
218207
name=cls.__name__

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp