From 97187d2a310024006a9dd7d6581f0ee27a6bab81 Mon Sep 17 00:00:00 2001 From: Max Jones <14077947+maxrjones@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:27:51 -0500 Subject: [PATCH 1/5] Align accepted filetypes with docstring description --- virtualizarr/backend.py | 2 +- virtualizarr/tests/test_backend.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/virtualizarr/backend.py b/virtualizarr/backend.py index a8e3b66a..9035e96e 100644 --- a/virtualizarr/backend.py +++ b/virtualizarr/backend.py @@ -184,7 +184,7 @@ def open_virtual_dataset( if filetype is not None: # if filetype is user defined, convert to FileType - filetype = FileType(filetype) + filetype = FileType(filetype.lower()) else: filetype = automatically_determine_filetype( filepath=filepath, reader_options=reader_options diff --git a/virtualizarr/tests/test_backend.py b/virtualizarr/tests/test_backend.py index 932fe7df..cfe2e299 100644 --- a/virtualizarr/tests/test_backend.py +++ b/virtualizarr/tests/test_backend.py @@ -385,6 +385,8 @@ def test_explicit_filetype(self, netcdf4_file): with pytest.raises(NotImplementedError): open_virtual_dataset(netcdf4_file, filetype="grib") + open_virtual_dataset(netcdf4_file, filetype="netCDF4") + def test_explicit_filetype_and_backend(self, netcdf4_file): with pytest.raises(ValueError): open_virtual_dataset( From 69122ce7accc5bf5f55fd9607f439ad970ceb649 Mon Sep 17 00:00:00 2001 From: Max Jones <14077947+maxrjones@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:37:42 -0500 Subject: [PATCH 2/5] Fix upstream test --- virtualizarr/backend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/virtualizarr/backend.py b/virtualizarr/backend.py index 9035e96e..7ec0f860 100644 --- a/virtualizarr/backend.py +++ b/virtualizarr/backend.py @@ -182,8 +182,8 @@ def open_virtual_dataset( if backend and filetype: raise ValueError("Cannot pass both a filetype and an explicit VirtualBackend") - if filetype is not None: - # if filetype is user defined, convert to FileType + if isinstance(filetype, str): + # if filetype is a user defined string, convert to FileType filetype = FileType(filetype.lower()) else: filetype = automatically_determine_filetype( From 974f8dc0947a6c74d3d9e1f0675aafb35c4a17b4 Mon Sep 17 00:00:00 2001 From: Max Jones <14077947+maxrjones@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:45:00 -0500 Subject: [PATCH 3/5] Fix docstring to match function declaration --- virtualizarr/backend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virtualizarr/backend.py b/virtualizarr/backend.py index 7ec0f860..03676d66 100644 --- a/virtualizarr/backend.py +++ b/virtualizarr/backend.py @@ -128,7 +128,7 @@ def open_virtual_dataset( ---------- filepath : str, default None File path to open as a set of virtualized zarr arrays. - filetype : FileType, default None + filetype : FileType or str, default None Type of file to be opened. Used to determine which kerchunk file format backend to use. Can be one of {'netCDF3', 'netCDF4', 'HDF', 'TIFF', 'GRIB', 'FITS', 'dmrpp', 'zarr_v3', 'kerchunk'}. If not provided will attempt to automatically infer the correct filetype from header bytes. From cb87a11d7b8d72793472d697e4bb746c6a0f0679 Mon Sep 17 00:00:00 2001 From: Max Jones <14077947+maxrjones@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:58:06 -0500 Subject: [PATCH 4/5] Only guess filetype if not set --- virtualizarr/backend.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/virtualizarr/backend.py b/virtualizarr/backend.py index 03676d66..c57dc432 100644 --- a/virtualizarr/backend.py +++ b/virtualizarr/backend.py @@ -182,13 +182,16 @@ def open_virtual_dataset( if backend and filetype: raise ValueError("Cannot pass both a filetype and an explicit VirtualBackend") - if isinstance(filetype, str): - # if filetype is a user defined string, convert to FileType - filetype = FileType(filetype.lower()) - else: + if filetype is None: filetype = automatically_determine_filetype( filepath=filepath, reader_options=reader_options ) + elif isinstance(filetype, str): + # if filetype is a user defined string, convert to FileType + filetype = FileType(filetype.lower()) + elif not isinstance(filetype, FileType): + raise ValueError("Filetype must be a valid string or FileType") + if backend: backend_cls = backend else: From da8aac48dffa728af90d7bb200aff921ec404f3c Mon Sep 17 00:00:00 2001 From: Max Jones <14077947+maxrjones@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:07:31 -0500 Subject: [PATCH 5/5] Test ValueError on wrong filetype argument type --- virtualizarr/tests/test_backend.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/virtualizarr/tests/test_backend.py b/virtualizarr/tests/test_backend.py index cfe2e299..211d719c 100644 --- a/virtualizarr/tests/test_backend.py +++ b/virtualizarr/tests/test_backend.py @@ -382,6 +382,9 @@ def test_explicit_filetype(self, netcdf4_file): with pytest.raises(ValueError): open_virtual_dataset(netcdf4_file, filetype="unknown") + with pytest.raises(ValueError): + open_virtual_dataset(netcdf4_file, filetype=ManifestArray) + with pytest.raises(NotImplementedError): open_virtual_dataset(netcdf4_file, filetype="grib")