Skip to content

Commit 7ed5620

Browse files
chore: add a lazy boolean attribute to RESTObject
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 an attribute 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 parent 1efb123 commit 7ed5620

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
if not isinstance(attrs, dict):
7577
raise GitlabParsingError(
@@ -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
if lazy is True:
107107
if TYPE_CHECKING:
108108
assert self._obj_cls._id_attr is not None
109-
return self._obj_cls(self, {self._obj_cls._id_attr: id})
109+
return self._obj_cls(self, {self._obj_cls._id_attr: id}, lazy=lazy)
110110
server_data = self.gitlab.http_get(path, **kwargs)
111111
if TYPE_CHECKING:
112112
assert not isinstance(server_data, requests.Response)
113-
return self._obj_cls(self, server_data)
113+
return self._obj_cls(self, server_data, lazy=lazy)
114114

115115

116116
class GetWithoutIdMixin(_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
assert isinstance(obj, FakeObject)
4545
assert obj.foo == "bar"
4646
assert obj.id == 42
47+
assert obj._lazy is False
4748
assert responses.assert_call_count(url, 1) is True
4849

4950

51+
def test_get_mixin_lazy(gl):
52+
class M(GetMixin, FakeManager):
53+
pass
54+
55+
url = "http://localhost/api/v4/tests/42"
56+
57+
mgr = M(gl)
58+
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
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+
assert isinstance(obj, FakeObject)
68+
assert not hasattr(obj, "foo")
69+
assert obj.id == 42
70+
assert obj._lazy is True
71+
# a `lazy` get does not make a network request
72+
assert len(rsps.calls) == 0
73+
74+
5075
@responses.activate
5176
def test_refresh_mixin(gl):
5277
class TestClass(RefreshMixin, FakeObject):

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