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

Commita7e8cfb

Browse files
chore: add alazy boolean attribute toRESTObject
This can be used to tell if a `RESTObject` was created using`lazy=True`.Add a message to the `AttributeError` if attribute access fails for aninstance created with `lazy=True`.
1 parentf6b6e18 commita7e8cfb

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

‎gitlab/base.py

Lines changed: 9 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
@@ -137,6 +140,12 @@ def __getattr__(self, name: str) -> Any:
137140
)
138141
+f"\n\n{_URL_ATTRIBUTE_ERROR}"
139142
)
143+
elifself._lazy:
144+
message=f"{message}\n\n"+textwrap.fill(
145+
f"If you tried to access object attributes returned from the server, "
146+
f"note that{self.__class__!r} was created as a `lazy` object and was "
147+
f"not initialized with any data."
148+
)
140149
raiseAttributeError(message)
141150

142151
def__setattr__(self,name:str,value:Any)->None:

‎gitlab/mixins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ def get(
135135
iflazyisTrue:
136136
ifTYPE_CHECKING:
137137
assertself._obj_cls._id_attrisnotNone
138-
returnself._obj_cls(self, {self._obj_cls._id_attr:id})
138+
returnself._obj_cls(self, {self._obj_cls._id_attr:id},lazy=lazy)
139139
server_data=self.gitlab.http_get(path,**kwargs)
140140
ifTYPE_CHECKING:
141141
assertnotisinstance(server_data,requests.Response)
142-
returnself._obj_cls(self,server_data)
142+
returnself._obj_cls(self,server_data,lazy=lazy)
143143

144144

145145
classGetWithoutIdMixin(HeadMixin,_RestManagerBase):

‎tests/unit/mixins/test_mixin_methods.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,52 @@ class M(GetMixin, FakeManager):
4545
assertisinstance(obj,FakeObject)
4646
assertobj.foo=="bar"
4747
assertobj.id==42
48+
assertobj._lazyisFalse
4849
assertresponses.assert_call_count(url,1)isTrue
4950

5051

52+
deftest_get_mixin_lazy(gl):
53+
classM(GetMixin,FakeManager):
54+
pass
55+
56+
url="http://localhost/api/v4/tests/42"
57+
58+
mgr=M(gl)
59+
withresponses.RequestsMock(assert_all_requests_are_fired=False)asrsps:
60+
rsps.add(
61+
method=responses.GET,
62+
url=url,
63+
json={"id":42,"foo":"bar"},
64+
status=200,
65+
match=[responses.matchers.query_param_matcher({})],
66+
)
67+
obj=mgr.get(42,lazy=True)
68+
assertisinstance(obj,FakeObject)
69+
assertnothasattr(obj,"foo")
70+
assertobj.id==42
71+
assertobj._lazyisTrue
72+
# a `lazy` get does not make a network request
73+
assertnotrsps.calls
74+
75+
76+
deftest_get_mixin_lazy_missing_attribute(gl):
77+
classFakeGetManager(GetMixin,FakeManager):
78+
pass
79+
80+
manager=FakeGetManager(gl)
81+
obj=manager.get(1,lazy=True)
82+
assertobj.id==1
83+
withpytest.raises(AttributeError)asexc:
84+
obj.missing_attribute
85+
# undo `textwrap.fill()`
86+
message=str(exc.value).replace("\n"," ")
87+
assert"'FakeObject' object has no attribute 'missing_attribute'"inmessage
88+
assert (
89+
"note that <class 'tests.unit.mixins.test_mixin_methods.FakeObject'> was "
90+
"created as a `lazy` object and was not initialized with any data."
91+
)inmessage
92+
93+
5194
@responses.activate
5295
deftest_head_mixin(gl):
5396
classM(GetMixin,FakeManager):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp