cache relpath and path_in_repo (#4053)

This commit is contained in:
Saugat Pachhai
2020-06-17 04:28:10 +03:00
committed by GitHub
parent 387867fbbe
commit 5d957b1830
2 changed files with 9 additions and 3 deletions
+5 -3
View File
@@ -3,7 +3,7 @@ import os
import string
from collections import defaultdict
from funcy import project
from funcy import cached_property, project
import dvc.dependency as dependency
import dvc.prompt as prompt
@@ -127,6 +127,8 @@ class Stage(params.StageParams):
@path.setter
def path(self, path):
self._path = path
self.__dict__.pop("path_in_repo", None)
self.__dict__.pop("relpath", None)
@property
def dvcfile(self):
@@ -172,11 +174,11 @@ class Stage(params.StageParams):
and self.path_in_repo == other.path_in_repo
)
@property
@cached_property
def path_in_repo(self):
return relpath(self.path, self.repo.root_dir)
@property
@cached_property
def relpath(self):
return relpath(self.path)
+4
View File
@@ -220,6 +220,10 @@ def test_stage_strings_representation(tmp_dir, dvc, run_copy):
folder = tmp_dir / "dir"
folder.mkdir()
with folder.chdir():
# `Stage` caches `relpath` results, forcing it to reset
stage1.path = stage1.path
stage2.path = stage2.path
rel_path = os.path.relpath(stage1.path)
assert stage1.addressing == rel_path
assert repr(stage1) == f"Stage: '{rel_path}'"