diff options
| author | Nejc Habjan <nejc.habjan@siemens.com> | 2022-07-20 21:17:42 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-20 21:17:42 +0200 |
| commit | 2c90fd0f317213a5a29bf6a2b63715a287e9fcfa (patch) | |
| tree | b856fc6cf2dfb93148a67655acd8054ecd177600 /gitlab | |
| parent | f6b6e18f96f4cdf67c8c53ae79e6a8259dcce9ee (diff) | |
| parent | a7e8cfbae8e53d2c4b1fb75d57d42f00db8abd81 (diff) | |
| download | gitlab-2c90fd0f317213a5a29bf6a2b63715a287e9fcfa.tar.gz | |
Merge pull request #2082 from python-gitlab/jlvillal/mark_lazy_state
chore: add a `lazy` boolean attribute to `RESTObject`
Diffstat (limited to 'gitlab')
| -rw-r--r-- | gitlab/base.py | 9 | ||||
| -rw-r--r-- | gitlab/mixins.py | 4 |
2 files changed, 11 insertions, 2 deletions
diff --git a/gitlab/base.py b/gitlab/base.py index 920617b..dd59240 100644 --- a/gitlab/base.py +++ b/gitlab/base.py @@ -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__( @@ -70,6 +71,7 @@ class RESTObject: attrs: Dict[str, Any], *, created_from_list: bool = False, + lazy: bool = False, ) -> None: if not isinstance(attrs, dict): raise GitlabParsingError( @@ -84,6 +86,7 @@ class RESTObject: "_updated_attrs": {}, "_module": importlib.import_module(self.__module__), "_created_from_list": created_from_list, + "_lazy": lazy, } ) self.__dict__["_parent_attrs"] = self.manager.parent_attrs @@ -137,6 +140,12 @@ class RESTObject: ) + 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: diff --git a/gitlab/mixins.py b/gitlab/mixins.py index 519e83f..f33a1fc 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -135,11 +135,11 @@ class GetMixin(HeadMixin, _RestManagerBase): if lazy is True: if TYPE_CHECKING: assert self._obj_cls._id_attr is not None - return self._obj_cls(self, {self._obj_cls._id_attr: id}) + return self._obj_cls(self, {self._obj_cls._id_attr: id}, lazy=lazy) server_data = self.gitlab.http_get(path, **kwargs) if TYPE_CHECKING: assert not isinstance(server_data, requests.Response) - return self._obj_cls(self, server_data) + return self._obj_cls(self, server_data, lazy=lazy) class GetWithoutIdMixin(HeadMixin, _RestManagerBase): |
