diff --git a/pyproject.toml b/pyproject.toml index 8421819e..91c9341f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,9 @@ hdf_reader = [ icechunk = [ "icechunk==0.1.0a8", ] +kerchunk_readers = [ + "astropy", +] test = [ "codecov", "fastparquet", @@ -53,6 +56,7 @@ test = [ "pandas-stubs", "pooch", "pre-commit", + "pytest-asyncio", "pytest-cov", "pytest-mypy", "pytest", @@ -87,6 +91,10 @@ datatree = ["py.typed"] files = "virtualizarr/**/*.py" show_error_codes = true +[[tool.mypy.overrides]] +module = "astropy.*" +ignore_missing_imports = true + [[tool.mypy.overrides]] module = "fsspec.*" ignore_missing_imports = true diff --git a/virtualizarr/readers/fits.py b/virtualizarr/readers/fits.py index e9119ccb..b1ecbbd1 100644 --- a/virtualizarr/readers/fits.py +++ b/virtualizarr/readers/fits.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Iterable, Mapping, Optional +from typing import Any, Iterable, Mapping from xarray import Dataset, Index, Variable @@ -23,10 +23,10 @@ def open_virtual_dataset( loadable_variables: Iterable[str] | None = None, decode_times: bool | None = None, indexes: Mapping[str, Index] | None = None, - virtual_backend_kwargs: Optional[dict] = None, - reader_options: Optional[dict] = None, + virtual_backend_kwargs: Mapping[str, Any] | None = None, + reader_options: Mapping[str, Any] | None = None, ) -> Dataset: - from kerchunk.fits import process_file + from virtualizarr.vendor.kerchunk.fits import process_file if virtual_backend_kwargs: raise NotImplementedError( @@ -34,7 +34,9 @@ def open_virtual_dataset( ) # handle inconsistency in kerchunk, see GH issue https://github.com/zarr-developers/VirtualiZarr/issues/160 - refs = KerchunkStoreRefs({"refs": process_file(filepath, **reader_options)}) + refs = KerchunkStoreRefs( + {"refs": process_file(filepath, **(reader_options or {}))} + ) # both group=None and group='' mean to read root group if group: diff --git a/virtualizarr/vendor/kerchunk/fits.py b/virtualizarr/vendor/kerchunk/fits.py index 3ac93aac..2ae31052 100644 --- a/virtualizarr/vendor/kerchunk/fits.py +++ b/virtualizarr/vendor/kerchunk/fits.py @@ -10,13 +10,13 @@ from virtualizarr.vendor.kerchunk.codecs import AsciiTableCodec, VarArrCodec try: - from astropy.io import fits - from astropy.wcs import WCS + from astropy.io import fits # noqa: F401 + from astropy.wcs import WCS # noqa: F401 except ModuleNotFoundError: # pragma: no cover raise ImportError( "astropy is required for kerchunking FITS files. Please install with " "`pip/conda install astropy`." - ) + ) from None logger = logging.getLogger("fits-to-zarr") @@ -162,7 +162,12 @@ def process_file( # TODO: we could sub-chunk on biggest dimension name = hdu.name or str(ext) arr = g.empty( - name, dtype=dtype, shape=shape, chunks=shape, compression=None, **kwargs + name=name, + dtype=dtype, + shape=shape, + chunks=shape, + compression=None, + **kwargs, ) arr.attrs.update( {