From 34186764bb4ebd5d4a499a92d8702d66a20f5c94 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Tue, 14 May 2024 16:09:37 -0500 Subject: [PATCH] fix: raise informative error if 'extras' key is missing for pyproject files --- src/rapids_dependency_file_generator/_config.py | 2 +- .../_rapids_dependency_file_generator.py | 11 +++++++---- .../pyproject-no-extras/dependencies.yaml | 12 ++++++++++++ tests/test_examples.py | 1 + tests/test_rapids_dependency_file_generator.py | 15 +++++++++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 tests/examples/pyproject-no-extras/dependencies.yaml diff --git a/src/rapids_dependency_file_generator/_config.py b/src/rapids_dependency_file_generator/_config.py index 2b746bd4..8d4a7f40 100644 --- a/src/rapids_dependency_file_generator/_config.py +++ b/src/rapids_dependency_file_generator/_config.py @@ -159,7 +159,7 @@ def _parse_extras(extras: dict[str, str]) -> FileExtras: def _parse_file(file_config: dict[str, typing.Any]) -> File: - def get_extras(): + def get_extras() -> typing.Union[FileExtras, None]: try: extras = file_config["extras"] except KeyError: diff --git a/src/rapids_dependency_file_generator/_rapids_dependency_file_generator.py b/src/rapids_dependency_file_generator/_rapids_dependency_file_generator.py index 7416ed9e..672131da 100644 --- a/src/rapids_dependency_file_generator/_rapids_dependency_file_generator.py +++ b/src/rapids_dependency_file_generator/_rapids_dependency_file_generator.py @@ -100,7 +100,7 @@ def make_dependency_file( output_dir: os.PathLike, conda_channels: list[str], dependencies: typing.Sequence[typing.Union[str, dict[str, list[str]]]], - extras: _config.FileExtras, + extras: typing.Union[_config.FileExtras, None], ): """Generate the contents of the dependency file. @@ -119,7 +119,7 @@ def make_dependency_file( CONDA. dependencies : Sequence[str | dict[str, list[str]]] The dependencies to include in the file. - extras : FileExtras + extras : FileExtras | None Any extra information provided for generating this dependency file. Returns @@ -145,17 +145,20 @@ def make_dependency_file( elif file_type == _config.Output.REQUIREMENTS: file_contents += "\n".join(dependencies) + "\n" elif file_type == _config.Output.PYPROJECT: + if extras is None: + raise ValueError("The 'extras' field must be provided for the 'pyproject' file type.") + if extras.table == "build-system": key = "requires" if extras.key is not None: raise ValueError( - "The 'key' field is not allowed for the 'pyproject' file type when " "'table' is 'build-system'." + "The 'key' field is not allowed for the 'pyproject' file type when 'table' is 'build-system'." ) elif extras.table == "project": key = "dependencies" if extras.key is not None: raise ValueError( - "The 'key' field is not allowed for the 'pyproject' file type when " "'table' is 'project'." + "The 'key' field is not allowed for the 'pyproject' file type when 'table' is 'project'." ) else: if extras.key is None: diff --git a/tests/examples/pyproject-no-extras/dependencies.yaml b/tests/examples/pyproject-no-extras/dependencies.yaml new file mode 100644 index 00000000..c2c64a4f --- /dev/null +++ b/tests/examples/pyproject-no-extras/dependencies.yaml @@ -0,0 +1,12 @@ +files: + beep_boop: + output: pyproject + includes: + - run_deps + pyproject_dir: . +dependencies: + run_deps: + common: + - output_types: [pyproject] + packages: + - fsspec>=0.6.0 diff --git a/tests/test_examples.py b/tests/test_examples.py index 55b0b456..273c6c79 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -17,6 +17,7 @@ "no-specific-match", "pyproject_matrix_multi", "pyproject_bad_key", + "pyproject-no-extras", ] EXAMPLE_FILES = [ pth diff --git a/tests/test_rapids_dependency_file_generator.py b/tests/test_rapids_dependency_file_generator.py index 33b60744..454b8fac 100644 --- a/tests/test_rapids_dependency_file_generator.py +++ b/tests/test_rapids_dependency_file_generator.py @@ -3,6 +3,7 @@ import yaml import tomlkit import pathlib +import pytest from rapids_dependency_file_generator import _config from rapids_dependency_file_generator._constants import cli_name @@ -79,6 +80,20 @@ def test_make_dependency_file(mock_relpath): assert env == header + "dep1\ndep2\n" +def test_make_dependency_file_should_raise_informative_error_when_extras_is_missing_for_pyproj(): + + current_dir = pathlib.Path(__file__).parent + with pytest.raises(ValueError, match=r"The 'extras' field must be provided for the 'pyproject' file type"): + make_dependency_files( + parsed_config=_config.load_config_from_file(current_dir / "examples" / "pyproject-no-extras" / "dependencies.yaml"), + file_keys=["beep_boop"], + output={_config.Output.PYPROJECT}, + matrix=None, + prepend_channels=[], + to_stdout=True + ) + + def test_make_dependency_files_should_choose_correct_pyproject_toml(capsys): current_dir = pathlib.Path(__file__).parent