Skip to content

Commit d15216c

Browse files
authored
UW-656 Run-time checks for bad uw execute arguments (#566)
1 parent 5dc92a3 commit d15216c

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/uwtools/api/driver.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from uwtools.drivers.support import graph
2424
from uwtools.drivers.support import tasks as _tasks
2525
from uwtools.logging import log
26+
from uwtools.strings import STR
2627
from uwtools.utils.api import ensure_data_source
2728

2829

@@ -62,22 +63,27 @@ def execute(
6263
"""
6364
if not (class_ := _get_driver_class(module, classname)):
6465
return False
66+
args = dict(locals())
6567
accepted = set(getfullargspec(class_).args)
66-
non_optional = {"cycle", "leadtime"}
67-
required = accepted & non_optional
68+
non_optional = {STR.cycle, STR.leadtime}
69+
for arg in sorted([STR.batch, *non_optional]):
70+
if args.get(arg) and arg not in accepted:
71+
log.error("%s does not accept argument '%s'", classname, arg)
72+
return False
73+
for arg in sorted(non_optional):
74+
if arg in accepted and args[arg] is None:
75+
log.error("%s requires argument '%s'", classname, arg)
76+
return False
6877
kwargs = dict(
6978
config=ensure_data_source(config, bool(stdin_ok)),
7079
dry_run=dry_run,
7180
key_path=key_path,
7281
schema_file=schema_file or Path(module).with_suffix(".jsonschema"),
7382
)
74-
for arg in sorted(non_optional):
75-
if arg in accepted and locals()[arg] is None:
76-
log.error("%s requires argument '%s'", classname, arg)
77-
return False
78-
for arg in sorted(["batch", *required]):
83+
required = non_optional & accepted
84+
for arg in sorted([STR.batch, *required]):
7985
if arg in accepted:
80-
kwargs[arg] = locals()[arg]
86+
kwargs[arg] = args[arg]
8187
driverobj = class_(**kwargs)
8288
log.debug("Instantiated %s with: %s", classname, kwargs)
8389
getattr(driverobj, task)()

src/uwtools/tests/api/test_driver.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,16 @@ def test__get_driver_module_implicit():
6565
pass
6666

6767

68+
@mark.parametrize("key,val", [("batch", True), ("leadtime", 6)])
69+
def test_execute_fail_bad_args(caplog, key, kwargs, val):
70+
kwargs.update({"cycle": dt.datetime.now(), key: val})
71+
assert driver_api.execute(**kwargs) is False
72+
assert logged(caplog, f"TestDriver does not accept argument '{key}'")
73+
74+
6875
def test_execute_fail_stdin_not_ok(kwargs):
6976
kwargs["config"] = None
77+
kwargs["cycle"] = dt.datetime.now()
7078
kwargs["stdin_ok"] = False
7179
with raises(UWError) as e:
7280
driver_api.execute(**kwargs)

0 commit comments

Comments
 (0)