Skip to content

fix(base): do not fail repr() on lazy objects #2081

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 11 additions & 4 deletions gitlab/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ def pprint(self) -> None:
def __repr__(self) -> str:
name = self.__class__.__name__

if (self._id_attr and self._repr_attr) and (self._id_attr != self._repr_attr):
if (self._id_attr and self._repr_value) and (self._id_attr != self._repr_attr):
return (
f"<{name} {self._id_attr}:{self.get_id()} "
f"{self._repr_attr}:{getattr(self, self._repr_attr)}>"
f"{self._repr_attr}:{self._repr_value}>"
)
if self._id_attr:
return f"<{name} {self._id_attr}:{self.get_id()}>"
if self._repr_attr:
return f"<{name} {self._repr_attr}:{getattr(self, self._repr_attr)}>"
if self._repr_value:
return f"<{name} {self._repr_attr}:{self._repr_value}>"

return f"<{name}>"

Expand Down Expand Up @@ -229,6 +229,13 @@ def get_id(self) -> Optional[Union[int, str]]:
return None
return getattr(self, self._id_attr)

@property
def _repr_value(self) -> Optional[str]:
"""Safely returns the human-readable resource name if present."""
if self._repr_attr is None or not hasattr(self, self._repr_attr):
return None
return getattr(self, self._repr_attr)

@property
def encoded_id(self) -> Optional[Union[int, str]]:
"""Ensure that the ID is url-encoded so that it can be safely used in a URL
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,19 @@ def test_dunder_str(self, fake_manager):
"<ReprObject id:1 name:fake>",
),
("name", "name", {"name": "fake"}, "<ReprObject name:fake>"),
("id", "name", {"id": 1}, "<ReprObject id:1>"),
(None, None, {}, "<ReprObject>"),
(None, "name", {"name": "fake"}, "<ReprObject name:fake>"),
(None, "name", {}, "<ReprObject>"),
],
ids=[
"GetMixin with id",
"GetMixin with id and _repr_attr",
"GetMixin with _repr_attr matching _id_attr",
"GetMixin with _repr_attr without _repr_attr value defined",
"GetWithoutIDMixin",
"GetWithoutIDMixin with _repr_attr",
"GetWithoutIDMixin with _repr_attr without _repr_attr value defined",
],
)
def test_dunder_repr(self, fake_manager, id_attr, repr_attr, attrs, expected_repr):
Expand Down
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