Skip to content

Commit 61a9fdc

Browse files
refactor: Update Live class to handle pathlib.Path object for dvcyaml argument.
1 parent 6888462 commit 61a9fdc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Diff for: src/dvclive/live.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(
8282
resume: bool = False,
8383
report: Literal["md", "notebook", "html", None] = None,
8484
save_dvc_exp: bool = True,
85-
dvcyaml: Optional[str] = "dvc.yaml",
85+
dvcyaml: Optional[str, Path] = "dvc.yaml",
8686
cache_images: bool = False,
8787
exp_name: Optional[str] = None,
8888
exp_message: Optional[str] = None,
@@ -104,11 +104,11 @@ def __init__(
104104
part of `Live.end()`. Defaults to `True`. If you are using DVCLive
105105
inside a DVC Pipeline and running with `dvc exp run`, the option will be
106106
ignored.
107-
dvcyaml (str | None): where to write dvc.yaml file, which adds DVC
107+
dvcyaml (str | Path | None): where to write dvc.yaml file, which adds DVC
108108
configuration for metrics, plots, and parameters as part of
109109
`Live.next_step()` and `Live.end()`. If `None`, no dvc.yaml file is
110110
written. Defaults to `"dvc.yaml"`. See `Live.make_dvcyaml()`.
111-
If a string like `"subdir/dvc.yaml"`, DVCLive will write the
111+
If a string or Path like `"subdir/dvc.yaml"`, DVCLive will write the
112112
configuration to that path (file must be named "dvc.yaml").
113113
If `False`, DVCLive will not write to "dvc.yaml" (useful if you are
114114
tracking DVCLive metrics, plots, and parameters independently and
@@ -265,10 +265,14 @@ def _init_dvc(self): # noqa: C901
265265
self._include_untracked.append(self.dir)
266266

267267
def _init_dvc_file(self) -> str:
268+
if isinstance(self._dvcyaml, Path):
269+
self._dvcyaml = str(self._dvcyaml)
268270
if isinstance(self._dvcyaml, str):
269271
if os.path.basename(self._dvcyaml) == "dvc.yaml":
270272
return self._dvcyaml
271273
raise InvalidDvcyamlError
274+
if self._dvcyaml:
275+
raise InvalidDvcyamlError
272276
return "dvc.yaml"
273277

274278
def _init_dvc_pipeline(self):
@@ -334,6 +338,8 @@ def _init_test(self):
334338
"""
335339
with tempfile.TemporaryDirectory() as dirpath:
336340
self._dir = os.path.join(dirpath, self._dir)
341+
if isinstance(self._dvcyaml, Path):
342+
self._dvcyaml = str(self._dvcyaml)
337343
if isinstance(self._dvcyaml, str):
338344
self._dvc_file = os.path.join(dirpath, self._dvcyaml)
339345
self._save_dvc_exp = False

Diff for: tests/test_dvc.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
import pytest
4+
from pathlib import Path
45
from dvc.exceptions import DvcException
56
from dvc.repo import Repo
67
from dvc.scm import NoSCM
@@ -220,12 +221,13 @@ def test_get_exp_name_duplicate(tmp_dir, mocked_dvc_repo, mocker, caplog):
220221
assert msg in caplog.text
221222

222223

223-
def test_test_mode(tmp_dir, monkeypatch, mocked_dvc_repo):
224+
@pytest.mark.parametrize("dvcyaml_path", ["dvc.yaml", Path("dvcyaml/dvc.yaml")])
225+
def test_test_mode(tmp_dir, monkeypatch, mocked_dvc_repo, dvcyaml_path):
224226
monkeypatch.setenv(DVCLIVE_TEST, "true")
225-
live = Live("dir", dvcyaml="dvc.yaml")
227+
live = Live("dir", dvcyaml=dvcyaml_path)
226228
live.make_dvcyaml()
227229
assert live._dir != "dir"
228230
assert live._dvc_file != "dvc.yaml"
229231
assert live._save_dvc_exp is False
230232
assert not os.path.exists("dir")
231-
assert not os.path.exists("dvc.yaml")
233+
assert not os.path.exists(dvcyaml_path)

0 commit comments

Comments
 (0)