diff --git a/dvc/stage/__init__.py b/dvc/stage/__init__.py index 866203001f..85615c43f8 100644 --- a/dvc/stage/__init__.py +++ b/dvc/stage/__init__.py @@ -3,7 +3,7 @@ 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 @@ def path(self): @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 @@ def __eq__(self, other): 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) diff --git a/tests/func/test_stage.py b/tests/func/test_stage.py index f2c00113b9..aabe42f072 100644 --- a/tests/func/test_stage.py +++ b/tests/func/test_stage.py @@ -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}'"