diff --git a/src/dvclive/live.py b/src/dvclive/live.py index c0b4aa8..f9726ed 100644 --- a/src/dvclive/live.py +++ b/src/dvclive/live.py @@ -82,7 +82,7 @@ def __init__( resume: bool = False, report: Literal["md", "notebook", "html", None] = None, save_dvc_exp: bool = True, - dvcyaml: Optional[str] = "dvc.yaml", + dvcyaml: Optional[str, Path] = "dvc.yaml", cache_images: bool = False, exp_name: Optional[str] = None, exp_message: Optional[str] = None, @@ -104,11 +104,11 @@ def __init__( part of `Live.end()`. Defaults to `True`. If you are using DVCLive inside a DVC Pipeline and running with `dvc exp run`, the option will be ignored. - dvcyaml (str | None): where to write dvc.yaml file, which adds DVC + dvcyaml (str | Path | None): where to write dvc.yaml file, which adds DVC configuration for metrics, plots, and parameters as part of `Live.next_step()` and `Live.end()`. If `None`, no dvc.yaml file is written. Defaults to `"dvc.yaml"`. See `Live.make_dvcyaml()`. - If a string like `"subdir/dvc.yaml"`, DVCLive will write the + If a string or Path like `"subdir/dvc.yaml"`, DVCLive will write the configuration to that path (file must be named "dvc.yaml"). If `False`, DVCLive will not write to "dvc.yaml" (useful if you are tracking DVCLive metrics, plots, and parameters independently and @@ -265,10 +265,14 @@ def _init_dvc(self): # noqa: C901 self._include_untracked.append(self.dir) def _init_dvc_file(self) -> str: + if isinstance(self._dvcyaml, Path): + self._dvcyaml = str(self._dvcyaml) if isinstance(self._dvcyaml, str): if os.path.basename(self._dvcyaml) == "dvc.yaml": return self._dvcyaml raise InvalidDvcyamlError + if self._dvcyaml: + raise InvalidDvcyamlError("DVC yaml path is invalid. Must be a string or a Path object.") return "dvc.yaml" def _init_dvc_pipeline(self): @@ -334,6 +338,8 @@ def _init_test(self): """ with tempfile.TemporaryDirectory() as dirpath: self._dir = os.path.join(dirpath, self._dir) + if isinstance(self._dvcyaml, Path): + self._dvcyaml = str(self._dvcyaml) if isinstance(self._dvcyaml, str): self._dvc_file = os.path.join(dirpath, self._dvcyaml) self._save_dvc_exp = False diff --git a/tests/test_dvc.py b/tests/test_dvc.py index bf263fc..e956323 100644 --- a/tests/test_dvc.py +++ b/tests/test_dvc.py @@ -1,6 +1,7 @@ import os import pytest +from pathlib import Path from dvc.exceptions import DvcException from dvc.repo import Repo from dvc.scm import NoSCM @@ -220,12 +221,13 @@ def test_get_exp_name_duplicate(tmp_dir, mocked_dvc_repo, mocker, caplog): assert msg in caplog.text -def test_test_mode(tmp_dir, monkeypatch, mocked_dvc_repo): +@pytest.mark.parametrize("dvcyaml_path", ["dvc.yaml", Path("dvcyaml/dvc.yaml")]) +def test_test_mode(tmp_dir, monkeypatch, mocked_dvc_repo, dvcyaml_path): monkeypatch.setenv(DVCLIVE_TEST, "true") - live = Live("dir", dvcyaml="dvc.yaml") + live = Live("dir", dvcyaml=dvcyaml_path) live.make_dvcyaml() assert live._dir != "dir" assert live._dvc_file != "dvc.yaml" assert live._save_dvc_exp is False assert not os.path.exists("dir") - assert not os.path.exists("dvc.yaml") + assert not os.path.exists(dvcyaml_path) \ No newline at end of file