metrics: adds support for strings in metrics

Fixes #7960
This commit is contained in:
paulourbano
2023-05-05 22:25:38 +02:00
committed by David de la Iglesia Castro
parent ac3eef439f
commit fae47e6122
3 changed files with 18 additions and 3 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ def _collect_metrics(repo, targets, recursive):
def _extract_metrics(metrics, path, rev):
if isinstance(metrics, (int, float)):
if isinstance(metrics, (int, float, str)):
return metrics
if not isinstance(metrics, dict):
+2
View File
@@ -26,6 +26,8 @@ def _diff_vals(old, new, with_unchanged):
res = {"old": old, "new": new}
if isinstance(new, (int, float)) and isinstance(old, (int, float)):
res["diff"] = new - old
elif isinstance(new, str) and isinstance(old, str):
res["diff"] = ""
return res
+15 -2
View File
@@ -32,7 +32,12 @@ def test_metrics_diff_yaml(tmp_dir, scm, dvc, run_copy_metrics):
_gen(2)
_gen(3)
expected = {"m.yaml": {"a.b.c": {"old": 1, "new": 3, "diff": 2}}}
expected = {
"m.yaml": {
"a.b.e": {"old": "1", "new": "3", "diff": ""},
"a.b.c": {"old": 1, "new": 3, "diff": 2},
}
}
assert dvc.metrics.diff(a_rev="HEAD~2") == expected
@@ -47,7 +52,12 @@ def test_metrics_diff_json(tmp_dir, scm, dvc, run_copy_metrics):
_gen(2)
_gen(3)
expected = {"m.json": {"a.b.c": {"old": 1, "new": 3, "diff": 2}}}
expected = {
"m.json": {
"a.b.e": {"old": "1", "new": "3", "diff": ""},
"a.b.c": {"old": 1, "new": 3, "diff": 2},
}
}
assert dvc.metrics.diff(a_rev="HEAD~2") == expected
@@ -78,6 +88,7 @@ def test_metrics_diff_broken_json(tmp_dir, scm, dvc, run_copy_metrics):
assert dvc.metrics.diff() == {
"m.json": {
"a.b.e": {"old": "3", "new": None},
"a.b.c": {"old": 1, "new": None},
"a.b.d": {"old": 1, "new": None},
}
@@ -96,6 +107,7 @@ def test_metrics_diff_new_metric(tmp_dir, scm, dvc, run_copy_metrics):
assert dvc.metrics.diff() == {
"m.json": {
"a.b.e": {"old": None, "new": "3"},
"a.b.c": {"old": None, "new": 1},
"a.b.d": {"old": None, "new": 1},
}
@@ -116,6 +128,7 @@ def test_metrics_diff_deleted_metric(tmp_dir, scm, dvc, run_copy_metrics):
assert dvc.metrics.diff() == {
"m.json": {
"a.b.e": {"old": "3", "new": None},
"a.b.c": {"old": 1, "new": None},
"a.b.d": {"old": 1, "new": None},
}