Skip to content

Fix: respect config when opening an existing array #2933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,11 @@
zarr_format = _metadata_dict["zarr_format"]
is_v3_array = zarr_format == 3 and _metadata_dict.get("node_type") == "array"
if is_v3_array or zarr_format == 2:
return AsyncArray(store_path=store_path, metadata=_metadata_dict)
return AsyncArray(
store_path=store_path,
metadata=_metadata_dict,
config=kwargs.pop("config", None),
)
except (AssertionError, FileNotFoundError, NodeTypeValidationError):
pass
return await open_group(store=store_path, zarr_format=zarr_format, mode=mode, **kwargs)
Expand Down Expand Up @@ -1247,13 +1251,24 @@

zarr_format = _handle_zarr_version_or_format(zarr_version=zarr_version, zarr_format=zarr_format)

config: ArrayConfigParams = kwargs.get("config", {})
if "order" in kwargs:
_warn_order_kwarg()
if "write_empty_chunks" in kwargs:
_warn_write_empty_chunks_kwarg()
if len(config) != 0:
msg = (

Check warning on line 1259 in src/zarr/api/asynchronous.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/api/asynchronous.py#L1258-L1259

Added lines #L1258 - L1259 were not covered by tests
"Both write_empty_chunks and config keyword arguments are set. "
"This is redundant. When both are set, write_empty_chunks will be ignored and "
"config will be used."
)
warnings.warn(UserWarning(msg), stacklevel=1)

Check warning on line 1264 in src/zarr/api/asynchronous.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/api/asynchronous.py#L1264

Added line #L1264 was not covered by tests
else:
_warn_write_empty_chunks_kwarg()

Check warning on line 1266 in src/zarr/api/asynchronous.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/api/asynchronous.py#L1266

Added line #L1266 was not covered by tests
# don't pop as this is only used for AsyncArray.open
config["write_empty_chunks"] = kwargs["write_empty_chunks"]

Check warning on line 1268 in src/zarr/api/asynchronous.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/api/asynchronous.py#L1268

Added line #L1268 was not covered by tests

try:
return await AsyncArray.open(store_path, zarr_format=zarr_format)
return await AsyncArray.open(store_path, zarr_format=zarr_format, config=config)
except FileNotFoundError:
if not store_path.read_only and mode in _CREATE_MODES:
overwrite = _infer_overwrite(mode)
Expand Down
5 changes: 4 additions & 1 deletion src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ async def open(
cls,
store: StoreLike,
zarr_format: ZarrFormat | None = 3,
config: ArrayConfigLike | None = None,
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
"""
Async method to open an existing Zarr array from a given store.
Expand All @@ -879,6 +880,8 @@ async def open(
The store containing the Zarr array.
zarr_format : ZarrFormat | None, optional
The Zarr format version (default is 3).
config : ArrayConfigLike, optional
Runtime configuration for the array.

Returns
-------
Expand All @@ -896,7 +899,7 @@ async def open(
metadata_dict = await get_array_metadata(store_path, zarr_format=zarr_format)
# TODO: remove this cast when we have better type hints
_metadata_dict = cast(ArrayV3MetadataDict, metadata_dict)
return cls(store_path=store_path, metadata=_metadata_dict)
return cls(store_path=store_path, metadata=_metadata_dict, config=config)

@property
def store(self) -> Store:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,43 @@ def test_resize_2d(store: MemoryStore, zarr_format: ZarrFormat) -> None:
assert new_shape == z[:].shape


@pytest.mark.parametrize("store", ["local"], indirect=True)
@pytest.mark.parametrize("open", ["open", "open_array"])
def test_append_config_passed(store: LocalStore, open: str, zarr_format: ZarrFormat) -> None:
Copy link
Contributor

@d-v-b d-v-b Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do our existing write_empty_chunks tests not work here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for context, it would probably be preferable to expand the scope of an existing test instead of adding a new one

z = zarr.create_array(
store=store,
name="test",
shape=(2,),
dtype=int,
fill_value=0,
chunks=(1,),
config={"write_empty_chunks": True},
overwrite=True,
zarr_format=zarr_format,
)
z[:] = 0

def assert_correct_files_written(expected: list[str]) -> None:
"""Helper to compare written files"""
if zarr_format == 2:
actual = [f.name for f in store.root.rglob("test/*")]
else:
actual = [f.name for f in store.root.rglob("test/c/*")]
actual = [f for f in actual if f not in [".zattrs", ".zarray", "zarr.json"]]
assert sorted(expected) == sorted(actual)

assert_correct_files_written(["0", "1"])

# parametrized over open and open_array
z = getattr(zarr, open)(store, path="test", config={"write_empty_chunks": True}, fill_value=0)
z.resize((4,))
assert_correct_files_written(["0", "1"])
z[2:] = 0
assert_correct_files_written(["0", "1", "2", "3"])
z[:] = 0
assert_correct_files_written(["0", "1", "2", "3"])


@pytest.mark.parametrize("store", ["memory"], indirect=True)
def test_append_1d(store: MemoryStore, zarr_format: ZarrFormat) -> None:
a = np.arange(105)
Expand Down