s/foreach..in/foreach..do (#4928)

This commit is contained in:
Saugat Pachhai
2020-11-20 19:47:58 +05:45
committed by GitHub
parent c65fdcabae
commit 133018af78
4 changed files with 22 additions and 22 deletions
+7 -7
View File
@@ -36,7 +36,7 @@ WDIR_KWD = "wdir"
DEFAULT_PARAMS_FILE = ParamsDependency.DEFAULT_PARAMS_FILE
PARAMS_KWD = "params"
FOREACH_KWD = "foreach"
IN_KWD = "in"
DO_KWD = "do"
SET_KWD = "set"
DEFAULT_SENTINEL = object()
@@ -119,12 +119,12 @@ class DataResolver:
def _resolve_entry(self, name: str, definition):
context = Context.clone(self.global_ctx)
if FOREACH_KWD in definition:
assert IN_KWD in definition
assert DO_KWD in definition
self.set_context_from(
context, definition.get(SET_KWD, {}), source=[name, "set"]
)
return self._foreach(
context, name, definition[FOREACH_KWD], definition[IN_KWD]
context, name, definition[FOREACH_KWD], definition[DO_KWD]
)
try:
@@ -206,9 +206,9 @@ class DataResolver:
format_and_raise(exc, f"'stages.{name}.wdir'", self.relpath)
return self.wdir / str(wdir)
def _foreach(self, context: Context, name: str, foreach_data, in_data):
def _foreach(self, context: Context, name: str, foreach_data, do_data):
iterable = self._resolve_foreach_data(context, name, foreach_data)
args = (context, name, in_data, iterable)
args = (context, name, do_data, iterable)
it = (
range(len(iterable))
if not isinstance(iterable, Mapping)
@@ -217,7 +217,7 @@ class DataResolver:
gen = (self._each_iter(*args, i) for i in it)
return join(gen)
def _each_iter(self, context: Context, name: str, in_data, iterable, key):
def _each_iter(self, context: Context, name: str, do_data, iterable, key):
value = iterable[key]
c = Context.clone(context)
suffix = c["item"] = value
@@ -226,7 +226,7 @@ class DataResolver:
generated = f"{name}{JOIN}{suffix}"
try:
return self._resolve_stage(c, generated, in_data)
return self._resolve_stage(c, generated, do_data)
except ContextError as exc:
# pylint: disable=no-member
if isinstance(exc, MergeError) and exc.key in self._inserted_keys(
+2 -2
View File
@@ -3,7 +3,7 @@ from voluptuous import Any, Optional, Required, Schema
from dvc import dependency, output
from dvc.hash_info import HashInfo
from dvc.output import CHECKSUMS_SCHEMA, BaseOutput
from dvc.parsing import FOREACH_KWD, IN_KWD, SET_KWD, VARS_KWD
from dvc.parsing import DO_KWD, FOREACH_KWD, SET_KWD, VARS_KWD
from dvc.stage.params import StageParams
STAGES = "stages"
@@ -83,7 +83,7 @@ STAGE_DEFINITION = {
FOREACH_IN = {
Optional(SET_KWD): dict,
Required(FOREACH_KWD): Any(dict, list, str),
Required(IN_KWD): STAGE_DEFINITION,
Required(DO_KWD): STAGE_DEFINITION,
}
SINGLE_PIPELINE_STAGE_SCHEMA = {str: Any(STAGE_DEFINITION, FOREACH_IN)}
MULTI_STAGE_SCHEMA = {
+5 -5
View File
@@ -254,7 +254,7 @@ def test_simple_foreach_loop(tmp_dir, dvc):
"stages": {
"build": {
"foreach": iterable,
"in": {"cmd": "python script.py ${item}"},
"do": {"cmd": "python script.py ${item}"},
}
}
}
@@ -277,7 +277,7 @@ def test_foreach_loop_dict(tmp_dir, dvc):
"stages": {
"build": {
"foreach": iterable["models"],
"in": {"cmd": "python script.py ${item.thresh}"},
"do": {"cmd": "python script.py ${item.thresh}"},
}
}
}
@@ -303,7 +303,7 @@ def test_foreach_loop_templatized(tmp_dir, dvc):
"stages": {
"build": {
"foreach": "${models}",
"in": {"cmd": "python script.py --thresh ${item.thresh}"},
"do": {"cmd": "python script.py --thresh ${item.thresh}"},
}
},
}
@@ -381,7 +381,7 @@ def test_set_with_foreach(tmp_dir, dvc):
"build": {
"set": {"items": items},
"foreach": "${items}",
"in": {"cmd": "command --value ${item}"},
"do": {"cmd": "command --value ${item}"},
}
}
}
@@ -407,7 +407,7 @@ def test_set_with_foreach_and_on_stage_definition(tmp_dir, dvc):
"build": {
"set": {"data": "${models}"},
"foreach": "${data}",
"in": {
"do": {
"set": {"thresh": "${item.thresh}"},
"cmd": "command --value ${thresh}",
},
+8 -8
View File
@@ -139,7 +139,7 @@ def test_foreach_item_key_already_exists(
):
d = {
"vars": [local_vars],
"stages": {"build": {"foreach": "${models}", "in": {}}},
"stages": {"build": {"foreach": "${models}", "do": {}}},
}
resolver = DataResolver(repo, tmp_dir, d)
@@ -152,7 +152,7 @@ def test_foreach_item_key_already_exists(
def test_foreach_interpolation_key_does_not_exist(tmp_dir, repo):
d = {
"stages": {"build": {"foreach": "${modelss}", "in": {}}},
"stages": {"build": {"foreach": "${modelss}", "do": {}}},
}
resolver = DataResolver(repo, tmp_dir, d)
with pytest.raises(ResolveError) as exc_info:
@@ -165,7 +165,7 @@ def test_foreach_interpolation_key_does_not_exist(tmp_dir, repo):
def test_foreach_interpolation_key_error(tmp_dir, repo):
d = {
"stages": {"build": {"foreach": "${models[123}", "in": {}}},
"stages": {"build": {"foreach": "${models[123}", "do": {}}},
}
resolver = DataResolver(repo, tmp_dir, d)
with pytest.raises(ResolveError) as exc_info:
@@ -195,7 +195,7 @@ def test_foreach_interpolation_key_error(tmp_dir, repo):
def test_foreach_generated_errors(tmp_dir, repo, syn, msg):
d = {
"stages": {
"build": {"foreach": "${models}", "in": {"cmd": f"echo {syn}"}}
"build": {"foreach": "${models}", "do": {"cmd": f"echo {syn}"}}
},
}
resolver = DataResolver(repo, tmp_dir, d)
@@ -222,7 +222,7 @@ def test_foreach_generated_local_vars_error(tmp_dir, repo, vars_, msg):
"stages": {
"build": {
"foreach": "${models}",
"in": {"vars": vars_, "cmd": "echo ${item}"},
"do": {"vars": vars_, "cmd": "echo ${item}"},
}
},
}
@@ -253,7 +253,7 @@ def test_foreach_wdir_interpolation_issues(tmp_dir, repo, wdir, msg):
"stages": {
"build": {
"foreach": "${models}",
"in": {"wdir": wdir, "cmd": "echo ${item}"},
"do": {"wdir": wdir, "cmd": "echo ${item}"},
}
},
}
@@ -268,7 +268,7 @@ def test_foreach_wdir_interpolation_issues(tmp_dir, repo, wdir, msg):
@pytest.mark.parametrize("foreach", ["${models.bar}", "foo"])
def test_foreach_expects_dict_or_list(tmp_dir, repo, foreach):
d = {
"stages": {"build": {"foreach": foreach, "in": {}}},
"stages": {"build": {"foreach": foreach, "do": {}}},
}
resolver = DataResolver(repo, tmp_dir, d)
with pytest.raises(ResolveError) as exc_info:
@@ -315,7 +315,7 @@ def test_item_key_in_generated_stage_vars(tmp_dir, repo, foreach, redefine):
"stages": {
"build": {
"foreach": foreach,
"in": {"cmd": "echo ${item}", "vars": [{redefine: "value"}]},
"do": {"cmd": "echo ${item}", "vars": [{redefine: "value"}]},
}
},
}