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

Experiments: allow setting of list parameters #4769

Merged
merged 3 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion dvc/repo/experiments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,14 @@ def _update_params(self, params: dict):
# recursive dict update
def _update(dict_, other):
for key, value in other.items():
if isinstance(dict_, list) and key.isdigit():
key = int(key)
if isinstance(value, Mapping):
dict_[key] = _update(dict_.get(key, {}), value)
if isinstance(dict_, list):
fallback_value = dict_[key]
else:
fallback_value = dict_.get(key, {})
dict_[key] = _update(fallback_value, value)
else:
dict_[key] = value
return dict_
Expand Down
29 changes: 29 additions & 0 deletions tests/func/experiments/test_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ def test_update_with_pull(tmp_dir, scm, dvc, mocker):
assert exp_scm.has_rev(rev)


@pytest.mark.parametrize(
"change, expected",
[
["foo.1.baz=3", "foo: [bar: 1, baz: 3]"],
["foo.0=bar", "foo: [bar, baz: 2]"],
["foo.1=- baz\n- goo", "foo: [bar: 1, [baz, goo]]"],
],
)
def test_modify_list_param(tmp_dir, scm, dvc, mocker, change, expected):
tmp_dir.gen("copy.py", COPY_SCRIPT)
tmp_dir.gen("params.yaml", "foo: [bar: 1, baz: 2]")
stage = dvc.run(
cmd="python copy.py params.yaml metrics.yaml",
metrics_no_cache=["metrics.yaml"],
params=["foo"],
name="copy-file",
)
scm.add(["dvc.yaml", "dvc.lock", "copy.py", "params.yaml", "metrics.yaml"])
scm.commit("init")

new_mock = mocker.spy(dvc.experiments, "new")
dvc.experiments.run(stage.addressing, params=[change])

new_mock.assert_called_once()
assert (
tmp_dir / ".dvc" / "experiments" / "metrics.yaml"
).read_text().strip() == expected


def test_checkout(tmp_dir, scm, dvc):
tmp_dir.gen("copy.py", COPY_SCRIPT)
tmp_dir.gen("params.yaml", "foo: 1")
Expand Down