From 254cf83310c0b2e18361a44a5eaa065528026ace Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Wed, 22 Apr 2020 02:15:46 +0800 Subject: [PATCH] metrics diff error message in --no-scm projects (#3650) * fix #3605 * changed exception raise position * Updated error msg * message changed * metrics diff: output edits per per https://github.com/iterative/dvc/pull/3650 and https://github.com/iterative/dvc/pull/3650#pullrequestreview-396837915 * Update tests/func/test_metrics.py Co-Authored-By: Ruslan Kuprieiev * NoSCM now had a default attribute getting method `NoSCM` Object now raise `NoSCMError` when calling attributes which only in `Git` Object * Joined lines Mail change test * tests: diff: use NoSCMError Co-authored-by: myname Co-authored-by: karajan1001 Co-authored-by: Jorge Orpinel Co-authored-by: Ruslan Kuprieiev Co-authored-by: Ruslan Kuprieiev --- dvc/repo/diff.py | 4 ---- dvc/scm/__init__.py | 5 +++-- dvc/scm/base.py | 10 ++++++++++ tests/func/test_diff.py | 4 +++- tests/func/test_metrics.py | 11 +++++++++++ 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/dvc/repo/diff.py b/dvc/repo/diff.py index d18389e05..5a83ed800 100644 --- a/dvc/repo/diff.py +++ b/dvc/repo/diff.py @@ -1,8 +1,6 @@ import os -from dvc.exceptions import DvcException from dvc.repo import locked -from dvc.scm.git import Git from dvc.scm.tree import is_working_tree @@ -15,8 +13,6 @@ def diff(self, a_rev="HEAD", b_rev=None): the concept of `index`, but it keeps the same interface, thus, `dvc diff` would be the same as `dvc diff HEAD`. """ - if type(self.scm) is not Git: - raise DvcException("only supported for Git repositories") def _paths_checksums(): """ diff --git a/dvc/scm/__init__.py b/dvc/scm/__init__.py index dbf524cc7..9a0b0f4a0 100644 --- a/dvc/scm/__init__.py +++ b/dvc/scm/__init__.py @@ -1,13 +1,14 @@ """Manages source control systems (e.g. Git).""" -from dvc.scm.base import Base +from dvc.scm.base import Base, NoSCMError from dvc.scm.git import Git # Syntactic sugar to signal that this is an actual implementation for a DVC # project under no SCM control. class NoSCM(Base): - pass + def __getattr__(self, name): + raise NoSCMError def SCM( diff --git a/dvc/scm/base.py b/dvc/scm/base.py index 017263de1..e1bedc242 100644 --- a/dvc/scm/base.py +++ b/dvc/scm/base.py @@ -24,6 +24,16 @@ class RevError(SCMError): pass +class NoSCMError(SCMError): + def __init__(self): + msg = ( + "Only supported for Git repositories. If you're " + "seeing this error in a Git repo, try updating the DVC " + "configuration with `dvc config core.no_scm false`." + ) + super().__init__(msg) + + class Base(object): """Base class for source control management driver implementations.""" diff --git a/tests/func/test_diff.py b/tests/func/test_diff.py index 4c3a660dd..96776d6fe 100644 --- a/tests/func/test_diff.py +++ b/tests/func/test_diff.py @@ -14,9 +14,11 @@ def digest(text): def test_no_scm(tmp_dir, dvc): + from dvc.scm.base import NoSCMError + tmp_dir.dvc_gen("file", "text") - with pytest.raises(DvcException, match=r"only supported for Git repos"): + with pytest.raises(NoSCMError): dvc.diff() diff --git a/tests/func/test_metrics.py b/tests/func/test_metrics.py index fab19c86d..f50972fe2 100644 --- a/tests/func/test_metrics.py +++ b/tests/func/test_metrics.py @@ -12,6 +12,7 @@ from dvc.main import main from dvc.repo import Repo as DvcRepo from dvc.repo.metrics.show import NO_METRICS_FILE_AT_REFERENCE_WARNING from dvc.utils import relpath +from dvc.scm.base import NoSCMError from tests.basic_env import TestDvcGit @@ -1005,3 +1006,13 @@ def test_metrics_diff_deleted_metric(tmp_dir, scm, dvc): "a.b.e": {"old": "3", "new": None}, } } + + +def test_metrics_without_scm(tmp_dir, dvc): + metrics = {"acc": 0.97, "recall": 0.95} + metrics_name = "metrics.json" + tmp_dir.gen({metrics_name: json.dumps(metrics)}) + dvc.add(metrics_name) + dvc.metrics.add(metrics_name) + with pytest.raises(NoSCMError): + dvc.metrics.diff()