From 9bf0b3000f6b10bf32b721a64bf0538edb0e8065 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Thu, 5 Oct 2023 19:04:59 +0000 Subject: [PATCH] Standardize behaviour: error if directory doesn't exist, error if no objects passed --- anndata/experimental/merge.py | 7 +++++-- anndata/tests/test_concatenate_disk.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/anndata/experimental/merge.py b/anndata/experimental/merge.py index 83108dd03..2413b3348 100644 --- a/anndata/experimental/merge.py +++ b/anndata/experimental/merge.py @@ -527,7 +527,7 @@ def concat_on_disk( Name: count, dtype: int64 """ if len(in_files) == 0: - return + raise ValueError("No objects to concatenate.") # Argument normalization if pairwise: @@ -537,8 +537,11 @@ def concat_on_disk( merge = resolve_merge_strategy(merge) uns_merge = resolve_merge_strategy(uns_merge) + out_file = Path(out_file) - out_file.parent.mkdir(parents=True, exist_ok=True) + if not out_file.parent.exists(): + raise FileNotFoundError(f"Parent directory of {out_file} does not exist.") + if isinstance(in_files, Mapping): if keys is not None: raise TypeError( diff --git a/anndata/tests/test_concatenate_disk.py b/anndata/tests/test_concatenate_disk.py index 0192df452..f9eab9540 100644 --- a/anndata/tests/test_concatenate_disk.py +++ b/anndata/tests/test_concatenate_disk.py @@ -250,3 +250,18 @@ def gen_index(n): def test_concatenate_obsm_inner(obsm_adatas, tmp_path, file_format): assert_eq_concat_on_disk(obsm_adatas, tmp_path, file_format, join="inner") + + +def test_output_dir_exists(tmp_path): + in_pth = tmp_path / "in.h5ad" + out_pth = tmp_path / "does_not_exist" / "out.h5ad" + + AnnData(X=np.ones((5, 1))).write_h5ad(in_pth) + + with pytest.raises(FileNotFoundError, match=f"{out_pth}"): + concat_on_disk([in_pth], out_pth) + + +def test_failure_w_no_args(tmp_path): + with pytest.raises(ValueError, match="No objects to concatenate"): + concat_on_disk([], tmp_path / "out.h5ad")