Skip to content
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
6 changes: 4 additions & 2 deletions dvc/repo/metrics/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from dvc.scm import NoSCMError
from dvc.utils import error_handler, errored_revisions, onerror_collect
from dvc.utils.collections import ensure_list
from dvc.utils.serialize import load_yaml
from dvc.utils.serialize import LOADERS

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -71,7 +71,9 @@ def _extract_metrics(metrics, path, rev):

@error_handler
def _read_metric(path, fs, rev, **kwargs):
val = load_yaml(path, fs=fs)
suffix = fs.path.suffix(path).lower()
loader = LOADERS[suffix]
val = loader(path, fs=fs)
Comment on lines +74 to +76
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocker of this P.R. but I think we are repeating these same 3 lines in a few places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, It'd be nice to refactor this. But there's two main ways the suffix is obtained: fs.path.suffix(fs_path) and os.path.splitext(fs_path). Is it safe to assume they are and always will be equivalent? Should one be preferred over the other?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to assume they are and always will be equivalent? Should one be preferred over the other?

I think it would always be the same. Anyhow, I think we can leave that out of this P.R.

val = _extract_metrics(val, path, rev)
return val or {}

Expand Down
14 changes: 12 additions & 2 deletions tests/func/metrics/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dvc.exceptions import OverlappingOutputPathsError
from dvc.repo import Repo
from dvc.utils.fs import remove
from dvc.utils.serialize import YAMLFileCorruptedError
from dvc.utils.serialize import JSONFileCorruptedError, YAMLFileCorruptedError


def test_show_simple(tmp_dir, dvc, run_copy_metrics):
Expand All @@ -31,6 +31,16 @@ def test_show(tmp_dir, dvc, run_copy_metrics):
}


def test_show_toml(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("metrics_t.toml", "[foo]\nbar = 1.2")
run_copy_metrics(
"metrics_t.toml", "metrics.toml", metrics=["metrics.toml"]
)
assert dvc.metrics.show() == {
"": {"data": {"metrics.toml": {"data": {"foo": {"bar": 1.2}}}}}
}


def test_show_targets(tmp_dir, dvc, run_copy_metrics):
tmp_dir.gen("metrics_t.yaml", "foo: 1.1")
run_copy_metrics(
Expand Down Expand Up @@ -218,7 +228,7 @@ def test_show_malformed_metric(tmp_dir, scm, dvc, caplog):
dvc.metrics.show(targets=["metric.json"])[""]["data"]["metric.json"][
"error"
],
YAMLFileCorruptedError,
JSONFileCorruptedError,
)


Expand Down