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

Commit7ed5620

Browse files
chore: add alazy boolean attribute toRESTObject
This can be used to tell if a `RESTObject` was created using`lazy=True`.Possible future uses for this will be if someone tries to access anattribute that does not exist they can be notified it is a "lazy"object, like we do for "list" generated objects.Another future use could be to give a warning if the attribute that`_repr_attr` is pointing to is missing and the object is not "lazy".
1 parent1efb123 commit7ed5620

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

‎gitlab/base.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class RESTObject:
6262
_parent_attrs:Dict[str,Any]
6363
_repr_attr:Optional[str]=None
6464
_updated_attrs:Dict[str,Any]
65+
_lazy:bool
6566
manager:"RESTManager"
6667

6768
def__init__(
@@ -70,6 +71,7 @@ def __init__(
7071
attrs:Dict[str,Any],
7172
*,
7273
created_from_list:bool=False,
74+
lazy:bool=False,
7375
)->None:
7476
ifnotisinstance(attrs,dict):
7577
raiseGitlabParsingError(
@@ -84,6 +86,7 @@ def __init__(
8486
"_updated_attrs": {},
8587
"_module":importlib.import_module(self.__module__),
8688
"_created_from_list":created_from_list,
89+
"_lazy":lazy,
8790
}
8891
)
8992
self.__dict__["_parent_attrs"]=self.manager.parent_attrs

‎gitlab/mixins.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ def get(
106106
iflazyisTrue:
107107
ifTYPE_CHECKING:
108108
assertself._obj_cls._id_attrisnotNone
109-
returnself._obj_cls(self, {self._obj_cls._id_attr:id})
109+
returnself._obj_cls(self, {self._obj_cls._id_attr:id},lazy=lazy)
110110
server_data=self.gitlab.http_get(path,**kwargs)
111111
ifTYPE_CHECKING:
112112
assertnotisinstance(server_data,requests.Response)
113-
returnself._obj_cls(self,server_data)
113+
returnself._obj_cls(self,server_data,lazy=lazy)
114114

115115

116116
classGetWithoutIdMixin(_RestManagerBase):

‎tests/unit/mixins/test_mixin_methods.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,34 @@ class M(GetMixin, FakeManager):
4444
assertisinstance(obj,FakeObject)
4545
assertobj.foo=="bar"
4646
assertobj.id==42
47+
assertobj._lazyisFalse
4748
assertresponses.assert_call_count(url,1)isTrue
4849

4950

51+
deftest_get_mixin_lazy(gl):
52+
classM(GetMixin,FakeManager):
53+
pass
54+
55+
url="http://localhost/api/v4/tests/42"
56+
57+
mgr=M(gl)
58+
withresponses.RequestsMock(assert_all_requests_are_fired=False)asrsps:
59+
rsps.add(
60+
method=responses.GET,
61+
url=url,
62+
json={"id":42,"foo":"bar"},
63+
status=200,
64+
match=[responses.matchers.query_param_matcher({})],
65+
)
66+
obj=mgr.get(42,lazy=True)
67+
assertisinstance(obj,FakeObject)
68+
assertnothasattr(obj,"foo")
69+
assertobj.id==42
70+
assertobj._lazyisTrue
71+
# a `lazy` get does not make a network request
72+
assertlen(rsps.calls)==0
73+
74+
5075
@responses.activate
5176
deftest_refresh_mixin(gl):
5277
classTestClass(RefreshMixin,FakeObject):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp