Skip to content

Commit

Permalink
fix: raise an informative error if pip map is passed for requirements…
Browse files Browse the repository at this point in the history
… file (#97)
  • Loading branch information
jameslamb authored May 16, 2024
1 parent bf7c342 commit 46f142c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 7 deletions.
17 changes: 13 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.3.0'
rev: 'v4.6.0'
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
Expand All @@ -10,14 +10,21 @@ repos:
- id: check-yaml
- id: debug-statements
- id: requirements-txt-fixer
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.10.0'
hooks:
- id: mypy
additional_dependencies: [types-PyYAML]
args: ["--config-file=pyproject.toml", "src/", "docs/"]
pass_filenames: false
- repo: https://github.com/asottile/pyupgrade
rev: 'v3.1.0'
rev: 'v3.15.2'
hooks:
- id: pyupgrade
args:
- --py38-plus
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.21.0
rev: 0.28.3
hooks:
- id: check-metaschema
files: ^src/rapids_dependency_file_generator/schema.json$
Expand All @@ -26,9 +33,11 @@ repos:
args: ["--schemafile", "src/rapids_dependency_file_generator/schema.json"]
- id: check-github-workflows
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
rev: v0.4.4
hooks:
- id: ruff
files: src/.*$
- id: ruff-format
files: src/.*$
default_language_version:
python: python3
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
]

templates_path = ["_templates"]
exclude_patterns = []
exclude_patterns: list[str] = []


# -- Options for HTML output -------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ version = {attr = "rapids_dependency_file_generator._version.__version__"}
[tool.isort]
profile = "black"

[tool.mypy]
ignore_missing_imports = true

[tool.ruff]
line-length = 120

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ def make_dependency_file(
}
)
elif file_type == _config.Output.REQUIREMENTS:
file_contents += "\n".join(dependencies) + "\n"
for dep in dependencies:
if isinstance(dep, dict):
raise ValueError(f"Map inputs like {dep} are not allowed for the 'requirements' file type.")

file_contents += f"{dep}\n"
elif file_type == _config.Output.PYPROJECT:
if extras is None:
raise ValueError("The 'extras' field must be provided for the 'pyproject' file type.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import json
import sys
import textwrap
import typing

import jsonschema
from jsonschema.exceptions import best_match

SCHEMA = json.loads(importlib.resources.files(__package__).joinpath("schema.json").read_bytes())


def validate_dependencies(dependencies) -> None:
def validate_dependencies(dependencies: dict[str, typing.Any]) -> None:
"""Validate a dictionary against the dependencies.yaml spec.
Parameters
Expand Down
14 changes: 14 additions & 0 deletions tests/examples/requirements-pip-dict/dependencies.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
files:
all_of_the_things:
output: requirements
includes:
- run_deps
requirements_dir: .
dependencies:
run_deps:
common:
- output_types: [requirements]
packages:
- fsspec>=0.6.0
- pip:
- pandas<1.0
1 change: 1 addition & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"pyproject_matrix_multi",
"pyproject_bad_key",
"pyproject-no-extras",
"requirements-pip-dict"
]
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 @@ -4,6 +4,7 @@
import tomlkit
import pathlib
import pytest
import re

from rapids_dependency_file_generator import _config
from rapids_dependency_file_generator._constants import cli_name
Expand Down Expand Up @@ -94,6 +95,20 @@ def test_make_dependency_file_should_raise_informative_error_when_extras_is_miss
)


def test_make_dependency_files_should_raise_informative_error_on_map_inputs_for_requirements():

current_dir = pathlib.Path(__file__).parent
with pytest.raises(ValueError, match=re.escape("Map inputs like {'pip': ['pandas<1.0']} are not allowed for the 'requirements' file type.")):
make_dependency_files(
parsed_config=_config.load_config_from_file(current_dir / "examples" / "requirements-pip-dict" / "dependencies.yaml"),
file_keys=["all_of_the_things"],
output={_config.Output.REQUIREMENTS},
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 46f142c

Please sign in to comment.