Repository: iterative/dvc

Issue:
Metrics show xpath does not override dvcfile xpath

I do have a Dvcfile with metrics, and for some period of time (a few commits) I defined a specific xpath in that file; Now, when I run a different xpath on `dvc metrics show`, the old xpath (and hense, different metric) shows up for the commits where dvcfile specified it:

```
>>> dvc metric show -x pe20

working tree:
        data/rentals/nyc/metrics.json: [88.15]
bin_bedroom:
        data/rentals/nyc/metrics.json: [88.25]
first-iteration:
        data/rentals/nyc/metrics.json: [88.13]
nhds:
        data/rentals/nyc/metrics.json: [7.45]
roosevelt:
        data/rentals/nyc/metrics.json: [7.11]
``` 

obviosly pe20 was > 88 for all the commits, it's just showing median error for the `nhds` and `roosevelt`


Discussion:
efiop: @pared heads-up: tests failed.

Patch:
diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py
index 758ba3a47..4b53f2222 100644
--- a/dvc/repo/metrics/show.py
+++ b/dvc/repo/metrics/show.py
@@ -188,10 +188,16 @@ def _collect_metrics(repo, path, recursive, typ, xpath, branch):
         if not o.metric:
             continue
 
+        # NOTE this case assumes that metric has been modified with
+        # `dvc metric modify -t {} -x {}`, so stage file should contain
+        # info what type metric is and what is its xpath
         if not typ and isinstance(o.metric, dict):
             t = o.metric.get(o.PARAM_METRIC_TYPE, typ)
-            x = o.metric.get(o.PARAM_METRIC_XPATH, xpath)
+            # NOTE user might want to check different xpath, hence xpath first
+            x = xpath or o.metric.get(o.PARAM_METRIC_XPATH)
         else:
+            # NOTE if metric has not been modified, user needs to provide
+            # both metric type and xpath
             t = typ
             x = xpath
 


Test patch:
diff --git a/tests/func/test_metrics.py b/tests/func/test_metrics.py
index 3e2286c88..4159c6461 100644
--- a/tests/func/test_metrics.py
+++ b/tests/func/test_metrics.py
@@ -831,3 +831,16 @@ class TestShouldDisplayMetricsEvenIfMetricIsMissing(object):
             in caplog.text
         )
         assert 0 == ret
+
+
+def test_show_xpath_should_override_stage_xpath(dvc_repo):
+    metric_file = "metric"
+    metric = {"m1": 0.1, "m2": 0.2}
+    with open(metric_file, "w") as fobj:
+        json.dump(metric, fobj)
+
+    dvc_repo.run(cmd="", overwrite=True, metrics=[metric_file])
+    dvc_repo.metrics.modify(metric_file, typ="json", xpath="m2")
+
+    result = dvc_repo.metrics.show(xpath="m1")
+    assert result == {"": {metric_file: [0.1]}}