From 298c61e946fbc83926c66cd69362a4ea1613566d Mon Sep 17 00:00:00 2001 From: Bradley Dice Date: Mon, 23 Sep 2024 12:26:59 -0500 Subject: [PATCH] feat: Correct behavior of matrix fallback priority, error on duplicate matrix selectors. (#110) --- .../_rapids_dependency_file_generator.py | 25 ++++++++++------- .../dependencies.yaml | 24 +++++++++++++++++ .../dependencies.yaml | 27 +++++++++++++++++++ .../output/expected/all_cuda-115.yaml | 8 ++++++ .../output/expected/all_cuda-118.yaml | 8 ++++++ tests/test_examples.py | 7 ++--- 6 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 tests/examples/duplicate-specific-matrix-entries/dependencies.yaml create mode 100644 tests/examples/specific-fallback-multiple-matches/dependencies.yaml create mode 100644 tests/examples/specific-fallback-multiple-matches/output/expected/all_cuda-115.yaml create mode 100644 tests/examples/specific-fallback-multiple-matches/output/expected/all_cuda-118.yaml 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 4fc186c1..b201e364 100644 --- a/src/rapids_dependency_file_generator/_rapids_dependency_file_generator.py +++ b/src/rapids_dependency_file_generator/_rapids_dependency_file_generator.py @@ -389,7 +389,20 @@ def make_dependency_files( if file_type not in specific_entry.output_types: continue - found = False + # Ensure that all specific matrices are unique + num_matrices = len(specific_entry.matrices) + num_unique = len( + { + frozenset(specific_matrices_entry.matrix.items()) + for specific_matrices_entry in specific_entry.matrices + } + ) + if num_matrices != num_unique: + err = f"All matrix entries must be unique. Found duplicates in '{include}':" + for specific_matrices_entry in specific_entry.matrices: + err += f"\n - {specific_matrices_entry.matrix}" + raise ValueError(err) + fallback_entry = None for specific_matrices_entry in specific_entry.matrices: # An empty `specific_matrices_entry["matrix"]` is @@ -403,18 +416,12 @@ def make_dependency_files( continue if should_use_specific_entry(matrix_combo, specific_matrices_entry.matrix): - # Raise an error if multiple specific entries - # (not including the fallback_entry) match a - # requested matrix combination. - if found: - raise ValueError(f"Found multiple matches for matrix {matrix_combo}") - found = True # A package list may be empty as a way to # indicate that for some matrix elements no # packages should be installed. dependencies.extend(specific_matrices_entry.packages or []) - - if not found: + break + else: if fallback_entry: dependencies.extend(fallback_entry.packages) else: diff --git a/tests/examples/duplicate-specific-matrix-entries/dependencies.yaml b/tests/examples/duplicate-specific-matrix-entries/dependencies.yaml new file mode 100644 index 00000000..54a262f6 --- /dev/null +++ b/tests/examples/duplicate-specific-matrix-entries/dependencies.yaml @@ -0,0 +1,24 @@ +files: + all: + output: conda + conda_dir: output/actual + matrix: + cuda: ["11.5", "11.8"] + includes: + - cudatoolkit +channels: + - rapidsai + - conda-forge +dependencies: + cudatoolkit: + specific: + - output_types: conda + matrices: + - matrix: + cuda: "11.5" + packages: + - cudatoolkit=11.5 + - matrix: + cuda: "11.5" + packages: + - cudatoolkit=11.5 diff --git a/tests/examples/specific-fallback-multiple-matches/dependencies.yaml b/tests/examples/specific-fallback-multiple-matches/dependencies.yaml new file mode 100644 index 00000000..0d33b4f8 --- /dev/null +++ b/tests/examples/specific-fallback-multiple-matches/dependencies.yaml @@ -0,0 +1,27 @@ +files: + all: + output: conda + conda_dir: output/actual + matrix: + cuda: ["11.5", "11.8"] + includes: + - cudatoolkit +channels: + - rapidsai + - conda-forge +dependencies: + cudatoolkit: + specific: + - output_types: conda + matrices: + - matrix: + cuda: "11.5" + packages: + - cudatoolkit=11.5 + - matrix: + cuda: "11.*" + packages: + - cudatoolkit=11.* + - matrix: + packages: + - cudatoolkit diff --git a/tests/examples/specific-fallback-multiple-matches/output/expected/all_cuda-115.yaml b/tests/examples/specific-fallback-multiple-matches/output/expected/all_cuda-115.yaml new file mode 100644 index 00000000..f9ab85dc --- /dev/null +++ b/tests/examples/specific-fallback-multiple-matches/output/expected/all_cuda-115.yaml @@ -0,0 +1,8 @@ +# This file is generated by `rapids-dependency-file-generator`. +# To make changes, edit ../../dependencies.yaml and run `rapids-dependency-file-generator`. +channels: +- rapidsai +- conda-forge +dependencies: +- cudatoolkit=11.5 +name: all_cuda-115 diff --git a/tests/examples/specific-fallback-multiple-matches/output/expected/all_cuda-118.yaml b/tests/examples/specific-fallback-multiple-matches/output/expected/all_cuda-118.yaml new file mode 100644 index 00000000..f9f9290b --- /dev/null +++ b/tests/examples/specific-fallback-multiple-matches/output/expected/all_cuda-118.yaml @@ -0,0 +1,8 @@ +# This file is generated by `rapids-dependency-file-generator`. +# To make changes, edit ../../dependencies.yaml and run `rapids-dependency-file-generator`. +channels: +- rapidsai +- conda-forge +dependencies: +- cudatoolkit=11.* +name: all_cuda-118 diff --git a/tests/test_examples.py b/tests/test_examples.py index c47ca351..2c5294d4 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -14,11 +14,12 @@ # Erroneous examples raise runtime errors from the generator. _erroneous_examples = [ + "duplicate-specific-matrix-entries", "no-specific-match", - "pyproject_matrix_multi", - "pyproject_bad_key", "pyproject-no-extras", - "requirements-pip-dict" + "pyproject_bad_key", + "pyproject_matrix_multi", + "requirements-pip-dict", ] EXAMPLE_FILES = [ pth