Skip to content

Commit

Permalink
Align accepted filetypes with docstring description (#361)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
maxrjones and TomNicholas authored Jan 9, 2025
1 parent d4f6b98 commit bd010c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
13 changes: 8 additions & 5 deletions virtualizarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -182,13 +182,16 @@ 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
filetype = FileType(filetype)
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:
Expand Down
5 changes: 5 additions & 0 deletions virtualizarr/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,14 @@ 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")

open_virtual_dataset(netcdf4_file, filetype="netCDF4")

def test_explicit_filetype_and_backend(self, netcdf4_file):
with pytest.raises(ValueError):
open_virtual_dataset(
Expand Down

0 comments on commit bd010c4

Please sign in to comment.