Skip to content

Incorrect type hints for Dataset.to_zarr and DataArray.to_zarr #10444

Open
@bernd-stahl

Description

@bernd-stahl

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

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions