Skip to content

Commit bd010c4

Browse files
Align accepted filetypes with docstring description (#361)
* Align accepted filetypes with docstring description * Fix upstream test * Fix docstring to match function declaration * Only guess filetype if not set * Test ValueError on wrong filetype argument type --------- Co-authored-by: Tom Nicholas <[email protected]>
1 parent d4f6b98 commit bd010c4

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

virtualizarr/backend.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def open_virtual_dataset(
128128
----------
129129
filepath : str, default None
130130
File path to open as a set of virtualized zarr arrays.
131-
filetype : FileType, default None
131+
filetype : FileType or str, default None
132132
Type of file to be opened. Used to determine which kerchunk file format backend to use.
133133
Can be one of {'netCDF3', 'netCDF4', 'HDF', 'TIFF', 'GRIB', 'FITS', 'dmrpp', 'zarr_v3', 'kerchunk'}.
134134
If not provided will attempt to automatically infer the correct filetype from header bytes.
@@ -182,13 +182,16 @@ def open_virtual_dataset(
182182
if backend and filetype:
183183
raise ValueError("Cannot pass both a filetype and an explicit VirtualBackend")
184184

185-
if filetype is not None:
186-
# if filetype is user defined, convert to FileType
187-
filetype = FileType(filetype)
188-
else:
185+
if filetype is None:
189186
filetype = automatically_determine_filetype(
190187
filepath=filepath, reader_options=reader_options
191188
)
189+
elif isinstance(filetype, str):
190+
# if filetype is a user defined string, convert to FileType
191+
filetype = FileType(filetype.lower())
192+
elif not isinstance(filetype, FileType):
193+
raise ValueError("Filetype must be a valid string or FileType")
194+
192195
if backend:
193196
backend_cls = backend
194197
else:

virtualizarr/tests/test_backend.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,14 @@ def test_explicit_filetype(self, netcdf4_file):
383383
with pytest.raises(ValueError):
384384
open_virtual_dataset(netcdf4_file, filetype="unknown")
385385

386+
with pytest.raises(ValueError):
387+
open_virtual_dataset(netcdf4_file, filetype=ManifestArray)
388+
386389
with pytest.raises(NotImplementedError):
387390
open_virtual_dataset(netcdf4_file, filetype="grib")
388391

392+
open_virtual_dataset(netcdf4_file, filetype="netCDF4")
393+
389394
def test_explicit_filetype_and_backend(self, netcdf4_file):
390395
with pytest.raises(ValueError):
391396
open_virtual_dataset(

0 commit comments

Comments
 (0)