Skip to content

Commit

Permalink
Fix type conditional check and UnboundLocalError for `params_result…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvothecoder authored and forsyth2 committed Dec 27, 2023
1 parent c7edb1d commit 85d3b3d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 12 additions & 6 deletions e3sm_diags/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
from itertools import chain
from typing import List
from typing import List, Union

import e3sm_diags # noqa: F401
from e3sm_diags.e3sm_diags_driver import get_default_diags_path, main
Expand Down Expand Up @@ -47,7 +47,7 @@ def is_cfg_file_arg_set(self):

def run_diags(
self, parameters: List[CoreParameter], use_cfg: bool = True
) -> List[CoreParameter]:
) -> Union[List[CoreParameter], None]:
"""Run a set of diagnostics with a list of parameters.
Parameters
Expand All @@ -68,15 +68,16 @@ def run_diags(
Returns
-------
List[CoreParameter]
A list of parameter objects with their results.
Union[List[CoreParameter], None]
A list of parameter objects with their results (if successful).
Raises
------
RuntimeError
If a diagnostic run using a parameter fails for any reason.
"""
params = self.get_run_parameters(parameters, use_cfg)
params_results = None

if params is None or len(params) == 0:
raise RuntimeError(
Expand All @@ -89,7 +90,9 @@ def run_diags(
except Exception:
logger.exception("Error traceback:", exc_info=True)

move_log_to_prov_dir(params_results[0].results_dir)
# param_results might be None because the run(s) failed, so move
# the log using the `params[0].results_dir` instead.
move_log_to_prov_dir(params[0].results_dir)

return params_results

Expand Down Expand Up @@ -449,7 +452,10 @@ def _get_instance_of_param_class(self, cls, parameters):

for cls_type in class_types:
for p in parameters:
if isinstance(p, cls_type):
# NOTE: This conditional is used instead of
# `isinstance(p, cls_type)` because we want to check for exact
# type matching and exclude sub-class matching.
if type(p) is cls_type:
return p

msg = "There's weren't any class of types {} in your parameters."
Expand Down
8 changes: 6 additions & 2 deletions tests/integration/test_all_sets_image_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ def run_diags_and_get_results_dir() -> str:
params = _get_test_params()
results = runner.run_diags(params)

results_dir = results[0].results_dir
# If results is None then that means some/all diagnostic set(s) failed.
# We use params[0].results_dir to check if any diagnostic sets passed.
if results is not None:
results_dir = results[0].results_dir
else:
results_dir = params[0].results_dir

logger.info(f"results_dir={results_dir}")

return results_dir


Expand Down

0 comments on commit 85d3b3d

Please sign in to comment.