Skip to content

Commit 7adb62e

Browse files
committed
⚡️ Keep KeyError when user attempts to access attribute they didnt ask for
1 parent b2498f2 commit 7adb62e

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

simvue/api/objects/base.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import abc
99
import pathlib
10+
import types
1011
import typing
1112
import inspect
1213
import uuid
@@ -281,23 +282,14 @@ def _get_attribute(
281282
self,
282283
attribute: str,
283284
*,
284-
local_default: object | None = None,
285285
url: str | None = None,
286286
) -> object:
287287
"""Retrieve an attribute for the given object.
288288
289-
The argument 'local_default' refers to the value to return when
290-
retrieving objects of this type via 'get'. In this case, if a key
291-
is not present (likely due to the user specifying key=False on retrieval)
292-
we do not want to attempt to retrieve the value from the server, as doing
293-
so for every item would cause significant overhead. Instead we use this value.
294-
295289
Parameters
296290
----------
297291
attribute : str
298292
name of attribute to retrieve
299-
local_default : str | None, optional
300-
if specified, the default value to return if the object is in 'local' mode.
301293
url : str | None, optional
302294
alternative URL to use for retrieval.
303295
@@ -319,7 +311,7 @@ def _get_attribute(
319311
return self._staging[attribute]
320312
except KeyError as e:
321313
if self._local:
322-
return local_default
314+
raise e
323315
# If the key is not in staging, but the object is not in offline mode
324316
# retrieve from the server and update cache instead
325317
if not _offline_state and (
@@ -338,9 +330,6 @@ def _get_attribute(
338330
)
339331
return self._get(url=url)[attribute]
340332
except KeyError as e:
341-
if local_default:
342-
return local_default
343-
344333
if self._offline:
345334
raise AttributeError(
346335
f"A value for attribute '{attribute}' has "
@@ -740,9 +729,19 @@ def __str__(self) -> str:
740729

741730
def __repr__(self) -> str:
742731
_out_str = f"{self.__class__.__module__}.{self.__class__.__qualname__}("
743-
_out_str += ", ".join(
744-
f"{property}={getattr(self, property, 'N/A')!r}"
745-
for property in self._properties
746-
)
732+
_property_values: list[str] = []
733+
734+
for property in self._properties:
735+
try:
736+
_value = getattr(self, property)
737+
except KeyError:
738+
continue
739+
740+
if isinstance(_value, types.GeneratorType):
741+
continue
742+
743+
_property_values.append(f"{property}={_value!r}")
744+
745+
_out_str += ", ".join(_property_values)
747746
_out_str += ")"
748747
return _out_str

simvue/api/objects/run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def get_alert_details(self) -> typing.Generator[dict[str, typing.Any], None, Non
372372
raise RuntimeError(
373373
"Cannot get alert details from an offline run - use .alerts to access a list of IDs instead"
374374
)
375-
for alert in self._get_attribute("alerts", local_default=[]):
375+
for alert in self._get_attribute("alerts"):
376376
yield alert["alert"]
377377

378378
@property
@@ -457,7 +457,7 @@ def metrics(
457457
-------
458458
Generator[tuple[str, dict[str, int | float | bool]]
459459
"""
460-
yield from self._get_attribute("metrics", local_default={}).items()
460+
yield from self._get_attribute("metrics").items()
461461

462462
@property
463463
def events(

0 commit comments

Comments
 (0)