summaryrefslogtreecommitdiff
path: root/gitlab/base.py
diff options
context:
space:
mode:
authorJohn L. Villalovos <john@sodarock.com>2022-06-29 17:43:41 -0700
committerJohn L. Villalovos <john@sodarock.com>2022-06-29 17:43:41 -0700
commite7344709d931e2b254d225d77ca1474bc69971f8 (patch)
tree8f884ad708fe40839845df3458f72232551d33a5 /gitlab/base.py
parentca3b43892996890d9c976409393ee39f66c41b75 (diff)
downloadgitlab-e7344709d931e2b254d225d77ca1474bc69971f8.tar.gz
chore: simplify multi-nested try blocks
Instead of have a multi-nested series of try blocks. Convert it to a more readable series of `if` statements.
Diffstat (limited to 'gitlab/base.py')
-rw-r--r--gitlab/base.py76
1 files changed, 36 insertions, 40 deletions
diff --git a/gitlab/base.py b/gitlab/base.py
index 205a0f2..0ce76dc 100644
--- a/gitlab/base.py
+++ b/gitlab/base.py
@@ -101,47 +101,43 @@ class RESTObject:
self.__dict__["_module"] = importlib.import_module(module_name)
def __getattr__(self, name: str) -> Any:
- try:
+ if name in self.__dict__["_updated_attrs"]:
return self.__dict__["_updated_attrs"][name]
- except KeyError:
- try:
- value = self.__dict__["_attrs"][name]
-
- # If the value is a list, we copy it in the _updated_attrs dict
- # because we are not able to detect changes made on the object
- # (append, insert, pop, ...). Without forcing the attr
- # creation __setattr__ is never called, the list never ends up
- # in the _updated_attrs dict, and the update() and save()
- # method never push the new data to the server.
- # See https://github.com/python-gitlab/python-gitlab/issues/306
- #
- # note: _parent_attrs will only store simple values (int) so we
- # don't make this check in the next except block.
- if isinstance(value, list):
- self.__dict__["_updated_attrs"][name] = value[:]
- return self.__dict__["_updated_attrs"][name]
-
- return value
-
- except KeyError:
- try:
- return self.__dict__["_parent_attrs"][name]
- except KeyError as exc:
- message = (
- f"{type(self).__name__!r} object has no attribute {name!r}"
- )
- if self._created_from_list:
- message = (
- f"{message}\n\n"
- + textwrap.fill(
- f"{self.__class__!r} was created via a list() call and "
- f"only a subset of the data may be present. To ensure "
- f"all data is present get the object using a "
- f"get(object.id) call. For more details, see:"
- )
- + f"\n\n{_URL_ATTRIBUTE_ERROR}"
- )
- raise AttributeError(message) from exc
+
+ if name in self.__dict__["_attrs"]:
+ value = self.__dict__["_attrs"][name]
+ # If the value is a list, we copy it in the _updated_attrs dict
+ # because we are not able to detect changes made on the object
+ # (append, insert, pop, ...). Without forcing the attr
+ # creation __setattr__ is never called, the list never ends up
+ # in the _updated_attrs dict, and the update() and save()
+ # method never push the new data to the server.
+ # See https://github.com/python-gitlab/python-gitlab/issues/306
+ #
+ # note: _parent_attrs will only store simple values (int) so we
+ # don't make this check in the next block.
+ if isinstance(value, list):
+ self.__dict__["_updated_attrs"][name] = value[:]
+ return self.__dict__["_updated_attrs"][name]
+
+ return value
+
+ if name in self.__dict__["_parent_attrs"]:
+ return self.__dict__["_parent_attrs"][name]
+
+ message = f"{type(self).__name__!r} object has no attribute {name!r}"
+ if self._created_from_list:
+ message = (
+ f"{message}\n\n"
+ + textwrap.fill(
+ f"{self.__class__!r} was created via a list() call and "
+ f"only a subset of the data may be present. To ensure "
+ f"all data is present get the object using a "
+ f"get(object.id) call. For more details, see:"
+ )
+ + f"\n\n{_URL_ATTRIBUTE_ERROR}"
+ )
+ raise AttributeError(message)
def __setattr__(self, name: str, value: Any) -> None:
self.__dict__["_updated_attrs"][name] = value