diff --git a/dvc/command/metrics.py b/dvc/command/metrics.py index 8db3e949e..a4ef129c8 100644 --- a/dvc/command/metrics.py +++ b/dvc/command/metrics.py @@ -121,7 +121,13 @@ def add_parser(subparsers, parent_parser): "path", nargs="?", help="Path to a metric file or a directory." ) metrics_show_parser.add_argument( - "-t", "--type", help="Type of metrics (raw/json/tsv/htsv/csv/hcsv)." + "-t", + "--type", + help=( + "Type of metrics (json/tsv/htsv/csv/hcsv). " + "It can be detected by the file extension automatically. " + "Unsupported types will be treated as raw." + ), ) metrics_show_parser.add_argument( "-x", "--xpath", help="json/tsv/htsv/csv/hcsv path." diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py index e68ecee1a..4bbd5a16e 100644 --- a/dvc/repo/metrics/show.py +++ b/dvc/repo/metrics/show.py @@ -221,6 +221,8 @@ def _read_metrics(self, metrics, branch): res = {} for out, typ, xpath in metrics: assert out.scheme == "local" + if not typ: + typ = os.path.splitext(out.path.lower())[1].replace(".", "") if out.use_cache: metric = _read_metrics_filesystem( self.cache.local.get(out.checksum), diff --git a/tests/test_metrics.py b/tests/test_metrics.py index a1dfb9d05..bfa34366a 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -655,3 +655,58 @@ class TestCachedMetrics(TestDvc): def test_run(self): self._test_metrics(self._do_run) + + +class TestMetricsType(TestDvc): + branches = ["foo", "bar", "baz"] + files = [ + "metric", + "metric.txt", + "metric.json", + "metric.tsv", + "metric.htsv", + "metric.csv", + "metric.hcsv", + ] + xpaths = [None, None, "branch", "0,0", "0,branch", "0,0", "0,branch"] + + def setUp(self): + super(TestMetricsType, self).setUp() + self.dvc.scm.commit("init") + + for branch in self.branches: + self.dvc.scm.checkout(branch, create_new=True) + with open("metric", "w+") as fd: + fd.write(branch) + with open("metric.txt", "w+") as fd: + fd.write(branch) + with open("metric.json", "w+") as fd: + json.dump({"branch": branch}, fd) + with open("metric.csv", "w+") as fd: + fd.write(branch) + with open("metric.hcsv", "w+") as fd: + fd.write("branch\n") + fd.write(branch) + with open("metric.tsv", "w+") as fd: + fd.write(branch) + with open("metric.htsv", "w+") as fd: + fd.write("branch\n") + fd.write(branch) + self.dvc.run(metrics_no_cache=self.files, overwrite=True) + self.dvc.scm.add(self.files + ["metric.dvc"]) + self.dvc.scm.commit("metric") + + self.dvc.scm.checkout("master") + + def test_show(self): + for file_name, xpath in zip(self.files, self.xpaths): + self._do_show(file_name, xpath) + + def _do_show(self, file_name, xpath): + ret = self.dvc.metrics.show(file_name, xpath=xpath, all_branches=True) + self.assertEqual(len(ret), 3) + for branch in self.branches: + if isinstance(ret[branch][file_name], list): + self.assertSequenceEqual(ret[branch][file_name], [branch]) + else: + self.assertSequenceEqual(ret[branch][file_name], branch)