diff --git a/dvc/repo/metrics/diff.py b/dvc/repo/metrics/diff.py index 1ed20e3876..ef25d09d87 100644 --- a/dvc/repo/metrics/diff.py +++ b/dvc/repo/metrics/diff.py @@ -68,7 +68,11 @@ def _diff(old_raw, new_raw): if isinstance(new, dict) or isinstance(old, dict): return _diff_dicts(old, new) - return {"": _diff_vals(old, new)} + val_diff = _diff_vals(old, new) + if val_diff: + return {"": val_diff} + + return {} def _get_metrics(repo, *args, rev=None, **kwargs): diff --git a/tests/func/test_metrics.py b/tests/func/test_metrics.py index 8339b45ba3..fab19c86d6 100644 --- a/tests/func/test_metrics.py +++ b/tests/func/test_metrics.py @@ -895,6 +895,20 @@ def _gen(val): } +def test_metrics_diff_raw_unchanged(tmp_dir, scm, dvc): + def _gen(val): + tmp_dir.gen({"metrics": val}) + dvc.run(cmd="", metrics=["metrics"]) + dvc.scm.add(["metrics.dvc"]) + dvc.scm.commit(str(val)) + + _gen("raw 1") + _gen("raw 2") + _gen("raw 1") + + assert dvc.metrics.diff(a_rev="HEAD~2") == {} + + @pytest.mark.parametrize("xpath", [True, False]) def test_metrics_diff_json(tmp_dir, scm, dvc, xpath): def _gen(val): @@ -919,6 +933,25 @@ def _gen(val): assert expected == dvc.metrics.diff(a_rev="HEAD~2") +@pytest.mark.parametrize("xpath", [True, False]) +def test_metrics_diff_json_unchanged(tmp_dir, scm, dvc, xpath): + def _gen(val): + metrics = {"a": {"b": {"c": val, "d": 1, "e": str(val)}}} + tmp_dir.gen({"m.json": json.dumps(metrics)}) + dvc.run(cmd="", metrics=["m.json"]) + dvc.metrics.modify("m.json", typ="json") + if xpath: + dvc.metrics.modify("m.json", xpath="a.b.c") + dvc.scm.add(["m.json.dvc"]) + dvc.scm.commit(str(val)) + + _gen(1) + _gen(2) + _gen(1) + + assert dvc.metrics.diff(a_rev="HEAD~2") == {} + + def test_metrics_diff_broken_json(tmp_dir, scm, dvc): metrics = {"a": {"b": {"c": 1, "d": 1, "e": "3"}}} tmp_dir.gen({"m.json": json.dumps(metrics)})