Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: raise informative error if 'extras' key is missing for pyproject files #95

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading