Skip to content

Commit 254cf83

Browse files
karajan1001mynamejorgeorpinelefiop
authored
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 #3650 and #3650 (review) * Update tests/func/test_metrics.py Co-Authored-By: Ruslan Kuprieiev <[email protected]> * 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 <[email protected]> Co-authored-by: karajan1001 <[email protected]> Co-authored-by: Jorge Orpinel <[email protected]> Co-authored-by: Ruslan Kuprieiev <[email protected]> Co-authored-by: Ruslan Kuprieiev <[email protected]>
1 parent bca5edf commit 254cf83

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

dvc/repo/diff.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import os
22

3-
from dvc.exceptions import DvcException
43
from dvc.repo import locked
5-
from dvc.scm.git import Git
64
from dvc.scm.tree import is_working_tree
75

86

@@ -15,8 +13,6 @@ def diff(self, a_rev="HEAD", b_rev=None):
1513
the concept of `index`, but it keeps the same interface, thus,
1614
`dvc diff` would be the same as `dvc diff HEAD`.
1715
"""
18-
if type(self.scm) is not Git:
19-
raise DvcException("only supported for Git repositories")
2016

2117
def _paths_checksums():
2218
"""

dvc/scm/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""Manages source control systems (e.g. Git)."""
22

3-
from dvc.scm.base import Base
3+
from dvc.scm.base import Base, NoSCMError
44
from dvc.scm.git import Git
55

66

77
# Syntactic sugar to signal that this is an actual implementation for a DVC
88
# project under no SCM control.
99
class NoSCM(Base):
10-
pass
10+
def __getattr__(self, name):
11+
raise NoSCMError
1112

1213

1314
def SCM(

dvc/scm/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ class RevError(SCMError):
2424
pass
2525

2626

27+
class NoSCMError(SCMError):
28+
def __init__(self):
29+
msg = (
30+
"Only supported for Git repositories. If you're "
31+
"seeing this error in a Git repo, try updating the DVC "
32+
"configuration with `dvc config core.no_scm false`."
33+
)
34+
super().__init__(msg)
35+
36+
2737
class Base(object):
2838
"""Base class for source control management driver implementations."""
2939

tests/func/test_diff.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ def digest(text):
1414

1515

1616
def test_no_scm(tmp_dir, dvc):
17+
from dvc.scm.base import NoSCMError
18+
1719
tmp_dir.dvc_gen("file", "text")
1820

19-
with pytest.raises(DvcException, match=r"only supported for Git repos"):
21+
with pytest.raises(NoSCMError):
2022
dvc.diff()
2123

2224

tests/func/test_metrics.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from dvc.repo import Repo as DvcRepo
1313
from dvc.repo.metrics.show import NO_METRICS_FILE_AT_REFERENCE_WARNING
1414
from dvc.utils import relpath
15+
from dvc.scm.base import NoSCMError
1516
from tests.basic_env import TestDvcGit
1617

1718

@@ -1005,3 +1006,13 @@ def test_metrics_diff_deleted_metric(tmp_dir, scm, dvc):
10051006
"a.b.e": {"old": "3", "new": None},
10061007
}
10071008
}
1009+
1010+
1011+
def test_metrics_without_scm(tmp_dir, dvc):
1012+
metrics = {"acc": 0.97, "recall": 0.95}
1013+
metrics_name = "metrics.json"
1014+
tmp_dir.gen({metrics_name: json.dumps(metrics)})
1015+
dvc.add(metrics_name)
1016+
dvc.metrics.add(metrics_name)
1017+
with pytest.raises(NoSCMError):
1018+
dvc.metrics.diff()

0 commit comments

Comments
 (0)