From fae47e6122dc8a6888ff1f538ee821d325e709ae Mon Sep 17 00:00:00 2001 From: paulourbano Date: Sat, 29 Apr 2023 03:15:12 +0000 Subject: [PATCH] metrics: adds support for strings in metrics Fixes #7960 --- dvc/repo/metrics/show.py | 2 +- dvc/utils/diff.py | 2 ++ tests/func/metrics/test_diff.py | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py index fe9bd968e..f04556c38 100644 --- a/dvc/repo/metrics/show.py +++ b/dvc/repo/metrics/show.py @@ -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): diff --git a/dvc/utils/diff.py b/dvc/utils/diff.py index d4d6fe618..32391b13d 100644 --- a/dvc/utils/diff.py +++ b/dvc/utils/diff.py @@ -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 diff --git a/tests/func/metrics/test_diff.py b/tests/func/metrics/test_diff.py index 8cb3aa89c..594fb4bc8 100644 --- a/tests/func/metrics/test_diff.py +++ b/tests/func/metrics/test_diff.py @@ -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}, }