Skip to content

Commit

Permalink
fix: raise informative error if 'extras' key is missing for pyproject…
Browse files Browse the repository at this point in the history
… files (#95)
  • Loading branch information
jameslamb authored May 14, 2024
1 parent 6c52975 commit 1ff87f5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/rapids_dependency_file_generator/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions tests/examples/pyproject-no-extras/dependencies.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"no-specific-match",
"pyproject_matrix_multi",
"pyproject_bad_key",
"pyproject-no-extras",
]
EXAMPLE_FILES = [
pth
Expand Down
15 changes: 15 additions & 0 deletions tests/test_rapids_dependency_file_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1ff87f5

Please sign in to comment.