diff --git a/dvc/api/params.py b/dvc/api/params.py index d66b19a591..294a88efea 100644 --- a/dvc/api/params.py +++ b/dvc/api/params.py @@ -3,6 +3,7 @@ from funcy import first +from dvc.exceptions import DvcException from dvc.repo import Repo @@ -50,6 +51,9 @@ def params_show( Returns: Dict: See Examples below. + Raises: + DvcException: If no params are found in `repo`. + Examples: - No arguments. @@ -253,6 +257,9 @@ def _postprocess(params): if "workspace" in processed: del processed["workspace"] + if not processed: + raise DvcException("No params found") + return processed[first(processed)] with Repo.open(repo) as _repo: diff --git a/tests/func/api/test_params.py b/tests/func/api/test_params.py index 9a2fb0f83b..124089f4d0 100644 --- a/tests/func/api/test_params.py +++ b/tests/func/api/test_params.py @@ -3,6 +3,7 @@ import pytest from dvc import api +from dvc.exceptions import DvcException @pytest.fixture @@ -128,3 +129,19 @@ def test_params_show_repo(tmp_dir, erepo_dir): params=["foo"], ) assert api.params_show(repo=erepo_dir) == {"foo": 1} + + +def test_params_show_no_params_found(tmp_dir, dvc): + # Empty repo + with pytest.raises(DvcException, match="No params found"): + api.params_show() + + # params.yaml but no dvc.yaml + (tmp_dir / "params.yaml").dump({"foo": 1}) + assert api.params_show() == {"foo": 1} + + # dvc.yaml but no params.yaml + (tmp_dir / "params.yaml").unlink() + dvc.stage.add(name="echo", cmd="echo foo") + with pytest.raises(DvcException, match="No params found"): + api.params_show()