| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 41ceaca commit 1efb123
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -162,15 +162,15 @@ def pprint(self) -> None: | |||
| 162 | 162 | def __repr__(self) -> str: | |
| 163 | 163 | name = self.__class__.__name__ | |
| 164 | 164 | ||
| 165 | - if (self._id_attr and self._repr_attr) and (self._id_attr != self._repr_attr): | ||
| 165 | + if (self._id_attr and self._repr_value) and (self._id_attr != self._repr_attr): | ||
| 166 | 166 | return ( | |
| 167 | 167 | f"<{name} {self._id_attr}:{self.get_id()} " | |
| 168 | - f"{self._repr_attr}:{getattr(self, self._repr_attr)}>" | ||
| 168 | + f"{self._repr_attr}:{self._repr_value}>" | ||
| 169 | 169 | ) | |
| 170 | 170 | if self._id_attr: | |
| 171 | 171 | return f"<{name} {self._id_attr}:{self.get_id()}>" | |
| 172 | - if self._repr_attr: | ||
| 173 | - return f"<{name} {self._repr_attr}:{getattr(self, self._repr_attr)}>" | ||
| 172 | + if self._repr_value: | ||
| 173 | + return f"<{name} {self._repr_attr}:{self._repr_value}>" | ||
| 174 | 174 | ||
| 175 | 175 | return f"<{name}>" | |
| 176 | 176 | ||
@@ -229,6 +229,13 @@ def get_id(self) -> Optional[Union[int, str]]: | |||
| 229 | 229 | return None | |
| 230 | 230 | return getattr(self, self._id_attr) | |
| 231 | 231 | ||
| 232 | + @property | ||
| 233 | + def _repr_value(self) -> Optional[str]: | ||
| 234 | + """Safely returns the human-readable resource name if present.""" | ||
| 235 | + if self._repr_attr is None or not hasattr(self, self._repr_attr): | ||
| 236 | + return None | ||
| 237 | + return getattr(self, self._repr_attr) | ||
| 238 | + | ||
| 232 | 239 | @property | |
| 233 | 240 | def encoded_id(self) -> Optional[Union[int, str]]: | |
| 234 | 241 | """Ensure that the ID is url-encoded so that it can be safely used in a URL | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -251,15 +251,19 @@ def test_dunder_str(self, fake_manager): | |||
| 251 | 251 | "<ReprObject id:1 name:fake>", | |
| 252 | 252 | ), | |
| 253 | 253 | ("name", "name", {"name": "fake"}, "<ReprObject name:fake>"), | |
| 254 | + ("id", "name", {"id": 1}, "<ReprObject id:1>"), | ||
| 254 | 255 | (None, None, {}, "<ReprObject>"), | |
| 255 | 256 | (None, "name", {"name": "fake"}, "<ReprObject name:fake>"), | |
| 257 | + (None, "name", {}, "<ReprObject>"), | ||
| 256 | 258 | ], | |
| 257 | 259 | ids=[ | |
| 258 | 260 | "GetMixin with id", | |
| 259 | 261 | "GetMixin with id and _repr_attr", | |
| 260 | 262 | "GetMixin with _repr_attr matching _id_attr", | |
| 263 | + "GetMixin with _repr_attr without _repr_attr value defined", | ||
| 261 | 264 | "GetWithoutIDMixin", | |
| 262 | 265 | "GetWithoutIDMixin with _repr_attr", | |
| 266 | + "GetWithoutIDMixin with _repr_attr without _repr_attr value defined", | ||
| 263 | 267 | ], | |
| 264 | 268 | ) | |
| 265 | 269 | def test_dunder_repr(self, fake_manager, id_attr, repr_attr, attrs, expected_repr): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments