diff --git a/gitlab/base.py b/gitlab/base.py index 6e7d5c584..205a0f20e 100644 --- a/gitlab/base.py +++ b/gitlab/base.py @@ -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}>" @@ -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 diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py index 9c4b65f92..ea4ad9ee6 100644 --- a/tests/unit/test_base.py +++ b/tests/unit/test_base.py @@ -251,15 +251,19 @@ def test_dunder_str(self, fake_manager): "", ), ("name", "name", {"name": "fake"}, ""), + ("id", "name", {"id": 1}, ""), (None, None, {}, ""), (None, "name", {"name": "fake"}, ""), + (None, "name", {}, ""), ], 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): 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