- Notifications
You must be signed in to change notification settings - Fork674
chore: attempt to be more informative for missing attributes#1702
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletionsdocs/faq.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
38 changes: 34 additions & 4 deletionsgitlab/base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -16,9 +16,11 @@ | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| import importlib | ||
| import textwrap | ||
| from types import ModuleType | ||
| from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type | ||
| import gitlab | ||
| from gitlab import types as g_types | ||
| from gitlab.exceptions import GitlabParsingError | ||
| @@ -32,6 +34,12 @@ | ||
| ] | ||
| _URL_ATTRIBUTE_ERROR = ( | ||
| f"https://python-gitlab.readthedocs.io/en/{gitlab.__version__}/" | ||
| f"faq.html#attribute-error-list" | ||
| ) | ||
| class RESTObject(object): | ||
| """Represents an object built from server data. | ||
| @@ -45,13 +53,20 @@ class RESTObject(object): | ||
| _id_attr: Optional[str] = "id" | ||
| _attrs: Dict[str, Any] | ||
| _created_from_list: bool # Indicates if object was created from a list() action | ||
| _module: ModuleType | ||
| _parent_attrs: Dict[str, Any] | ||
| _short_print_attr: Optional[str] = None | ||
| _updated_attrs: Dict[str, Any] | ||
| manager: "RESTManager" | ||
| def __init__( | ||
| self, | ||
| manager: "RESTManager", | ||
| attrs: Dict[str, Any], | ||
| *, | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| created_from_list: bool = False, | ||
| ) -> None: | ||
| if not isinstance(attrs, dict): | ||
| raise GitlabParsingError( | ||
| "Attempted to initialize RESTObject with a non-dictionary value: " | ||
| @@ -64,6 +79,7 @@ def __init__(self, manager: "RESTManager", attrs: Dict[str, Any]) -> None: | ||
| "_attrs": attrs, | ||
| "_updated_attrs": {}, | ||
| "_module": importlib.import_module(self.__module__), | ||
| "_created_from_list": created_from_list, | ||
| } | ||
| ) | ||
| self.__dict__["_parent_attrs"] = self.manager.parent_attrs | ||
| @@ -106,8 +122,22 @@ def __getattr__(self, name: str) -> Any: | ||
| 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 | ||
| def __setattr__(self, name: str, value: Any) -> None: | ||
| self.__dict__["_updated_attrs"][name] = value | ||
| @@ -229,7 +259,7 @@ def __next__(self) -> RESTObject: | ||
| def next(self) -> RESTObject: | ||
| data = self._list.next() | ||
| return self._obj_cls(self.manager, data, created_from_list=True) | ||
| @property | ||
| def current_page(self) -> int: | ||
2 changes: 1 addition & 1 deletiongitlab/mixins.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletionstests/unit/test_base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.