Skip to content

Commit dc5ba64

Browse files
committed
refactor: replace asserts with explicit exceptions
Refactored code to replace assert statements with explicit exception raising for better error handling and clarity. Changed inappropriate asserts to TypeError or RuntimeError where applicable, and improved error messages for invalid input types and unexpected states.
1 parent 41c33c8 commit dc5ba64

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

cardano_node_tests/cluster_management/cluster_getter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,12 @@ def get_cluster_instance( # noqa: C901
724724
("singleton" tests).
725725
scriptsdir: Path to custom scripts for the cluster.
726726
"""
727-
assert not isinstance(lock_resources, str), "`lock_resources` can't be single string"
728-
assert not isinstance(use_resources, str), "`use_resources` can't be single string"
727+
if isinstance(lock_resources, str):
728+
msg = "`lock_resources` cannot be a string"
729+
raise TypeError(msg)
730+
if isinstance(use_resources, str):
731+
msg = "`use_resources` cannot be a string"
732+
raise TypeError(msg)
729733

730734
# Sanitize strings so they can be used in file names
731735
mark = resources.sanitize_res_name(mark)

cardano_node_tests/cluster_management/manager.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def _get_manager_fixture_line_str() -> str:
4242
"""Get `filename#lineno` of current fixture, called from contextmanager."""
4343
# Get past `cache_fixture` and `contextmanager` to the fixture
4444
calling_frame = inspect.currentframe().f_back.f_back.f_back # type: ignore
45-
assert calling_frame
45+
if not calling_frame:
46+
msg = "Couldn't get the calling frame."
47+
raise ValueError(msg)
4648
return helpers.get_line_str_from_frame(frame=calling_frame)
4749

4850

@@ -282,7 +284,7 @@ def _get_resources_from_paths(
282284
) -> list[str]:
283285
if from_set is not None and isinstance(from_set, str):
284286
msg = "`from_set` cannot be a string"
285-
raise AssertionError(msg)
287+
raise TypeError(msg)
286288

287289
resources = set(status_files.get_resources_from_path(paths=paths))
288290

@@ -383,7 +385,7 @@ def init(
383385
cluster_obj = self.cache.cluster_obj
384386
if not cluster_obj:
385387
msg = "`cluster_obj` not available, that cannot happen"
386-
raise AssertionError(msg)
388+
raise RuntimeError(msg)
387389
cluster_obj.cluster_id = self.cluster_instance_num
388390
cluster_obj._cluster_manager = self # type: ignore
389391
self._initialized = True
@@ -408,8 +410,8 @@ def get(
408410
# If you've ran into this issue, check that all the fixtures you use in the test are using
409411
# the same `cluster` fixture.
410412
if check_initialized and self._initialized:
411-
msg = "manager is already initialized"
412-
raise AssertionError(msg)
413+
msg = "Manager is already initialized"
414+
raise RuntimeError(msg)
413415

414416
self.init(
415417
mark=mark,
@@ -421,5 +423,7 @@ def get(
421423
)
422424

423425
cluster_obj = self.cache.cluster_obj
424-
assert cluster_obj
426+
if not cluster_obj:
427+
msg = "`cluster_obj` not available, that cannot happen"
428+
raise RuntimeError(msg)
425429
return cluster_obj

cardano_node_tests/cluster_management/resources_management.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class BaseFilter:
88
"""Base class for resource filters."""
99

1010
def __init__(self, resources: tp.Iterable[str]) -> None:
11-
assert not isinstance(resources, str), "`resources` can't be single string"
11+
if isinstance(resources, str):
12+
msg = "`resources` cannot be a string"
13+
raise TypeError(msg)
1214
self.resources = resources
1315

1416
def filter(self, unavailable: tp.Iterable[str], **kwargs: tp.Any) -> list[str]:
@@ -27,7 +29,9 @@ def filter(
2729
unavailable: tp.Iterable[str],
2830
**kwargs: tp.Any, # noqa: ARG002
2931
) -> list[str]:
30-
assert not isinstance(unavailable, str), "`unavailable` can't be single string"
32+
if isinstance(unavailable, str):
33+
msg = "`unavailable` cannot be a string"
34+
raise TypeError(msg)
3135

3236
usable = [r for r in self.resources if r not in unavailable]
3337
if not usable:

0 commit comments

Comments
 (0)