Repository: dask/dask
File: dask/array/core.py (Python)

Code before:
                    f"chunk (or shard) of the Zarr array, which in this case is "
                    f"{safe_chunk_size} bytes. "
                    f'E.g., dask.config.set({{"array.chunk-size": {safe_chunk_size}}})'
                )
                raise PerformanceWarning(msg)
                break
    if region is None:
        # Get the appropriate write granularity (shard shape if sharding, else chunk shape)
        arr = arr.rechunk(dask_write_chunks)
        regions = None
    else:
        from dask.array.slicing import new_blockdim, normalize_index

        index = normalize_index(region, z.shape)
        chunks = tuple(
            tuple(new_blockdim(s, c, r))
            for s, c, r in zip(z.shape, dask_write_chunks, index)
        )
        arr = arr.rechunk(chunks)
        regions = [region]
    return arr.store(
        z, lock=False, regions=regions, compute=compute, return_stored=return_stored
    )


def _setup_zarr_store(url, storage_options, **kwargs):
    """
    Set up a Zarr store for reading or writing, handling both Zarr v2 and v3.

    This function prepares a Zarr-compatible storage object (`store`) from a URL or existing
    store. It supports optional storage options for fsspec-based stores and automatically
    selects the appropriate store type depending on the Zarr version.

    Parameters
    ----------
    url: Zarr Array or str or MutableMapping
        Location of the data. A URL can include a protocol specifier like s3://
        for remote data. Can also be any MutableMapping instance, which should
        be serializable if used in multiple processes.
    storage_options: dict
        Any additional parameters for the storage backend (ignored for local
        paths)
    **kwargs:
        Passed to determine whether the store should be readonly by evaluating the following:
        'kwargs.pop("read_only", kwargs.pop("mode", "a") == "r")'

    Returns
    -------
    store : zarr.store.Store or original url
        A Zarr-compatible store object. Can be:
        - `zarr.storage.FsspecStore` for Zarr v3 with storage options

Review comment (suggestion):
```suggestion
def _setup_zarr_store(url: str, storage_options: dict[str, object] | None = None, **kwargs: object):
```

Code after:

    Returns
    -------
    store : zarr.store.Store or original url
        A Zarr-compatible store object. Can be:
        - `zarr.storage.FsspecStore` for Zarr v3 with storage options
        - `zarr.storage.FSStore` for Zarr v2 with storage options
        - The original URL/path if no storage options are provided
    """
    # Cannot directly import FSStore from storage.
    from zarr import storage

    if storage_options is not None:
        if _zarr_v3():
            read_only = kwargs.pop("read_only", kwargs.pop("mode", "a") == "r")
            store = storage.FsspecStore.from_url(
                url, read_only=read_only, storage_options=storage_options
            )
        else:
            store = storage.FSStore(url, **storage_options)
    else:
        store = url
    return store


def to_zarr(
    arr,
    url,
    component=None,
    storage_options=None,
    region=None,
    compute=True,
    return_stored=False,
    zarr_array_kwargs=None,
    zarr_read_kwargs=None,
    **kwargs,
):
    """Save array to the zarr storage format

    See https://zarr.readthedocs.io for details about the format.

    Parameters
    ----------
    arr: dask.array
        Data to store
    url: Zarr Array or str or MutableMapping
        Location of the data. A URL can include a protocol specifier like s3://
        for remote data. Can also be any MutableMapping instance, which should
        be serializable if used in multiple processes.
    component: str or None
        If the location is a zarr group rather than an array, this is the