Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache relpath on stage #4053

Merged
merged 1 commit into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions dvc/stage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
skshetry marked this conversation as resolved.
Show resolved Hide resolved
def relpath(self):
return relpath(self.path)

Expand Down
4 changes: 4 additions & 0 deletions tests/func/test_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Comment on lines 222 to +226
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugly hack, chdir is now not supported as we cache the relpath. It's fine for CLI use-case, but for API usecases, it sucks.

rel_path = os.path.relpath(stage1.path)
assert stage1.addressing == rel_path
assert repr(stage1) == f"Stage: '{rel_path}'"
Expand Down