xarray icon indicating copy to clipboard operation
xarray copied to clipboard

Incorrect type hints for Dataset.to_zarr and DataArray.to_zarr

Open bernd-stahl opened this issue 4 months ago • 2 comments
trafficstars

What happened?

The store parameter of the xr.core.Dataset.to_zarr method is annotated with the following type hint: MutableMapping | str | PathLike[str] | None

This causes a type checker error when passing an instance of zarr.abc.Store, which works just fine and seems like a sensible use case.

What did you expect to happen?

Passing an instance of zarr.abc.Store should not result in a type checker error.

Minimal Complete Verifiable Example

import xarray as xr
import zarr

store = zarr.storage.LocalStore('/tmp/foo.zarr')
ds = xr.Dataset()
ds.to_zarr(store, group='group', mode='w', consolidated=False, compute=False, write_empty_chunks=False)

MVCE confirmation

  • [x] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • [x] Complete example — the example is self-contained, including all data and the text of any traceback.
  • [x] Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • [x] New issue — a search of GitHub Issues suggests this is not a duplicate.
  • [x] Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

demo.py:6: error: No overload variant of "to_zarr" of "Dataset" matches argument types "LocalStore", "str", "str", "bool", "bool", "bool"  [call-overload]

Anything else we need to know?

The method does nothing but call xarray.backends.api.to_zarr, which takes a store parameter type-hinted as ZarrStoreLike, which in xarray.core.types is defined as: from zarr.storage import StoreLike as ZarrStoreLike and resolves to StoreLike = Store | StorePath | Path | str | dict[str, Buffer], so it could be reasonably expected to be allowed to pass a zarr.abc.store.Store to the Dataset instance method.

The same holds for zarr.core.DataArray.to_zarr.

It seems the type hints were not adapted when moving from Zarr v2 to v3, which uses the zarr.store.abc.Store abstraction instead of MutableMapping. `

Environment

INSTALLED VERSIONS

commit: None python: 3.12.10 (main, May 28 2025, 09:53:23) [GCC 15.1.1 20250425] python-bits: 64 OS: Linux OS-release: 6.12.34-1-MANJARO machine: x86_64 processor: byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.14.2 libnetcdf: 4.9.4-development

xarray: 2025.6.1 pandas: 2.3.0 numpy: 2.3.0 scipy: None netCDF4: 1.7.2 pydap: None h5netcdf: None h5py: None zarr: 3.0.8 cftime: 1.6.4.post1 nc_time_axis: None iris: None bottleneck: None dask: 2025.5.1 distributed: None matplotlib: None cartopy: None seaborn: None numbagg: None fsspec: 2025.5.1 cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: None pip: 25.1.1 conda: None pytest: 8.4.0 mypy: 1.16.1 IPython: None sphinx: 8.2.3

bernd-stahl avatar Jun 25 '25 14:06 bernd-stahl

Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!

welcome[bot] avatar Jun 25 '25 14:06 welcome[bot]

thanks!

generally we don't leave open issues for type hints, because they're unlikely to inspire someone who isn't affected by it to change it, and it's difficult to track when they've been completed

but PRs are very welcome! either to fix or even just to add a failing example, where it can be tracked...

max-sixty avatar Jun 25 '25 18:06 max-sixty