Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct ExecArgs Handling During RunSetting #517

Merged
merged 17 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions smartsim/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@

@exe_args.setter
def exe_args(self, value: t.Union[str, t.List[str], None]) -> None:
self._exe_args = self._build_exe_args(value)
if (
isinstance(value, str)
or isinstance(value, list)
and all(isinstance(i, str) for i in value)
):
self._exe_args = self._build_exe_args(value)
else:
raise TypeError("Executable arguments were not list of str or str.")

Check warning on line 131 in smartsim/settings/base.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/base.py#L131

Added line #L131 was not covered by tests

@property
def run_args(self) -> t.Dict[str, t.Union[int, str, float, None]]:
Expand Down Expand Up @@ -448,12 +455,16 @@
:type args: str | list[str]
:raises TypeError: if exe args are not strings
"""

if not isinstance(args, (list, str)):
amandarichardsonn marked this conversation as resolved.
Show resolved Hide resolved
raise TypeError("All elements in the list should be of type str")

Check warning on line 460 in smartsim/settings/base.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/base.py#L459-L460

Added lines #L459 - L460 were not covered by tests

amandarichardsonn marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(args, str):
args = args.split()

for arg in args:
if not isinstance(arg, str):
raise TypeError("Executable arguments should be a list of str")
if isinstance(args, list):
if not all(isinstance(arg, str) for arg in args):
raise TypeError("All elements in the list should be of type str")

Check warning on line 467 in smartsim/settings/base.py

View check run for this annotation

Codecov / codecov/patch

smartsim/settings/base.py#L465-L467

Added lines #L465 - L467 were not covered by tests

self._exe_args.extend(args)

Expand Down Expand Up @@ -538,20 +549,7 @@
if isinstance(exe_args, str):
return exe_args.split()
if isinstance(exe_args, list):
exe_args = copy.deepcopy(exe_args)
plain_type = all(isinstance(arg, (str)) for arg in exe_args)
if not plain_type:
nested_type = all(
all(isinstance(arg, (str)) for arg in exe_args_list)
for exe_args_list in exe_args
)
if not nested_type:
raise TypeError(
"Executable arguments were not list of str or str"
)
return exe_args
return exe_args
raise TypeError("Executable arguments were not list of str or str")
return []

def format_run_args(self) -> t.List[str]:
Expand Down
16 changes: 11 additions & 5 deletions tests/test_run_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,17 @@ def test_add_exe_args_list_of_mixed_lists_init():
exe_args = [["1", "2", 3], ["4", "5", 6]]

with pytest.raises(TypeError) as type_error:
settings = RunSettings("python", exe_args=exe_args)
_ = RunSettings("python", exe_args=exe_args)

assert "Executable arguments were not list of str or str" in type_error.value.args
assert "Executable arguments were not list of str or str." in type_error.value.args


def test_add_exe_args_list_of_str_lists_init():
"""Ensure that list[list[str]] pass validation"""
exe_args = [["1", "2", "3"], ["4", "5", "6"]]

settings = RunSettings("python", exe_args=exe_args)

assert settings.exe_args == exe_args
with pytest.raises(TypeError) as type_error:
amandarichardsonn marked this conversation as resolved.
Show resolved Hide resolved
settings = RunSettings("python", exe_args=exe_args)


def test_add_exe_args_list_of_str_lists():
Expand Down Expand Up @@ -323,6 +322,13 @@ def test_bad_exe_args_2():
_ = RunSettings("python", exe_args=exe_args)


def test_bad_exe_args_3():
"""test when user provides incorrect types to exe_args"""
exe_args = [["this", "should"], ["not", "work"]]
with pytest.raises(TypeError):
amandarichardsonn marked this conversation as resolved.
Show resolved Hide resolved
_ = RunSettings("python", exe_args=exe_args)


def test_set_args():
rs = RunSettings("python")
rs.set("str", "some-string")
Expand Down
Loading