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

chore: add alazy boolean attribute toRESTObject#2082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nejch merged 1 commit intomainfromjlvillal/mark_lazy_state
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletionsgitlab/base.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -62,6 +62,7 @@ class RESTObject:
_parent_attrs: Dict[str, Any]
_repr_attr: Optional[str] = None
_updated_attrs: Dict[str, Any]
_lazy: bool
manager: "RESTManager"

def __init__(
Expand All@@ -70,6 +71,7 @@ def __init__(
attrs: Dict[str, Any],
*,
created_from_list: bool = False,
lazy: bool = False,
) -> None:
if not isinstance(attrs, dict):
raise GitlabParsingError(
Expand All@@ -84,6 +86,7 @@ def __init__(
"_updated_attrs": {},
"_module": importlib.import_module(self.__module__),
"_created_from_list": created_from_list,
"_lazy": lazy,
}
)
self.__dict__["_parent_attrs"] = self.manager.parent_attrs
Expand DownExpand Up@@ -137,6 +140,12 @@ def __getattr__(self, name: str) -> Any:
)
+ f"\n\n{_URL_ATTRIBUTE_ERROR}"
)
elif self._lazy:
message = f"{message}\n\n" + textwrap.fill(
f"If you tried to access object attributes returned from the server, "
f"note that {self.__class__!r} was created as a `lazy` object and was "
f"not initialized with any data."
)
raise AttributeError(message)

def __setattr__(self, name: str, value: Any) -> None:
Expand Down
4 changes: 2 additions & 2 deletionsgitlab/mixins.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -135,11 +135,11 @@ def get(
iflazyisTrue:
ifTYPE_CHECKING:
assertself._obj_cls._id_attrisnotNone
returnself._obj_cls(self, {self._obj_cls._id_attr:id})
returnself._obj_cls(self, {self._obj_cls._id_attr:id},lazy=lazy)
server_data=self.gitlab.http_get(path,**kwargs)
ifTYPE_CHECKING:
assertnotisinstance(server_data,requests.Response)
returnself._obj_cls(self,server_data)
returnself._obj_cls(self,server_data,lazy=lazy)


classGetWithoutIdMixin(HeadMixin,_RestManagerBase):
Expand Down
43 changes: 43 additions & 0 deletionstests/unit/mixins/test_mixin_methods.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -45,9 +45,52 @@ class M(GetMixin, FakeManager):
assert isinstance(obj, FakeObject)
assert obj.foo == "bar"
assert obj.id == 42
assert obj._lazy is False
assert responses.assert_call_count(url, 1) is True


def test_get_mixin_lazy(gl):
class M(GetMixin, FakeManager):
pass

url = "http://localhost/api/v4/tests/42"

mgr = M(gl)
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add(
method=responses.GET,
url=url,
json={"id": 42, "foo": "bar"},
status=200,
match=[responses.matchers.query_param_matcher({})],
)
obj = mgr.get(42, lazy=True)
assert isinstance(obj, FakeObject)
assert not hasattr(obj, "foo")
assert obj.id == 42
assert obj._lazy is True
# a `lazy` get does not make a network request
assert not rsps.calls


def test_get_mixin_lazy_missing_attribute(gl):
class FakeGetManager(GetMixin, FakeManager):
pass

manager = FakeGetManager(gl)
obj = manager.get(1, lazy=True)
assert obj.id == 1
with pytest.raises(AttributeError) as exc:
obj.missing_attribute
# undo `textwrap.fill()`
message = str(exc.value).replace("\n", " ")
assert "'FakeObject' object has no attribute 'missing_attribute'" in message
assert (
"note that <class 'tests.unit.mixins.test_mixin_methods.FakeObject'> was "
"created as a `lazy` object and was not initialized with any data."
) in message


@responses.activate
def test_head_mixin(gl):
class M(GetMixin, FakeManager):
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp