Skip to content

Commit 11ed454

Browse files
committed
Remove unneeded StopIteration handling on object retrieval
1 parent c3ac7a4 commit 11ed454

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

simvue/api/objects/folder.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,12 @@ def get_folder_from_path(
284284
) -> Folder:
285285
_folders = Folder.get(filters=json.dumps([f"path == {path}"]), count=1)
286286

287-
try:
288-
_, _folder = next(_folders)
289-
except StopIteration as e:
290-
raise ObjectNotFoundError(obj_type="folder", name=path) from e
287+
if not (_first_entry := next(_folders, None)):
288+
raise ObjectNotFoundError(obj_type="folder", name=path)
289+
290+
_, _folder = _first_entry
291+
292+
if not _folder:
293+
raise ObjectNotFoundError(obj_type="folder", name=path)
294+
291295
return _folder # type: ignore

simvue/client.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,12 @@ def get_run_id_from_name(
125125
"""
126126
_runs = Run.get(filters=json.dumps([f"name == {name}"]))
127127

128-
try:
129-
_id, _ = next(_runs)
130-
except StopIteration as e:
131-
raise RuntimeError(
132-
"Could not collect ID - no run found with this name."
133-
) from e
128+
if not (_first_entry := next(_runs, None)):
129+
raise RuntimeError("Could not collect ID - no run found with this name.")
134130

135-
with contextlib.suppress(StopIteration):
136-
next(_runs)
131+
_id, _ = _first_entry
132+
133+
if next(_runs, None):
137134
raise RuntimeError(
138135
"Could not collect ID - more than one run exists with this name."
139136
)
@@ -325,11 +322,9 @@ def _get_folder_from_path(self, path: str) -> Folder | None:
325322
"""
326323
_folders = Folder.get(filters=json.dumps([f"path == {path}"]))
327324

328-
try:
329-
_, _folder = next(_folders)
330-
return _folder # type: ignore
331-
except StopIteration:
332-
return None
325+
_, _folder = next(_folders, (None, None))
326+
327+
return _folder
333328

334329
def _get_folder_id_from_path(self, path: str) -> str | None:
335330
"""Retrieve folder identifier for the specified path if found
@@ -346,13 +341,10 @@ def _get_folder_id_from_path(self, path: str) -> str | None:
346341
"""
347342
_ids = Folder.ids(filters=json.dumps([f"path == {path}"]))
348343

349-
try:
350-
_id = next(_ids)
351-
except StopIteration:
344+
if not (_id := next(_ids, None)):
352345
return None
353346

354-
with contextlib.suppress(StopIteration):
355-
next(_ids)
347+
if next(_ids, None):
356348
raise RuntimeError(
357349
f"Expected single folder match for '{path}', but found duplicate."
358350
)

0 commit comments

Comments
 (0)