Skip to content

Commit f71cdc1

Browse files
committed
fix(array): thread order parameter through to array __init__
1 parent 0eb4c8a commit f71cdc1

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

src/zarr/api/asynchronous.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ async def create(
712712
dtype: npt.DTypeLike | None = None,
713713
compressor: dict[str, JSON] | None = None, # TODO: default and type change
714714
fill_value: Any | None = 0, # TODO: need type
715-
order: MemoryOrder | None = None, # TODO: default change
715+
order: MemoryOrder | None = None,
716716
store: str | StoreLike | None = None,
717717
synchronizer: Any | None = None,
718718
overwrite: bool = False,
@@ -761,6 +761,7 @@ async def create(
761761
Default value to use for uninitialized portions of the array.
762762
order : {'C', 'F'}, optional
763763
Memory layout to be used within each chunk.
764+
Default is set in Zarr's config (`array.order`).
764765
store : Store or str
765766
Store or path to directory in file system or name of zip file.
766767
synchronizer : object, optional
@@ -834,12 +835,6 @@ async def create(
834835
else:
835836
chunk_shape = shape
836837

837-
if order is not None:
838-
warnings.warn(
839-
"order is deprecated, use config `array.order` instead",
840-
DeprecationWarning,
841-
stacklevel=2,
842-
)
843838
if synchronizer is not None:
844839
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
845840
if chunk_store is not None:
@@ -889,6 +884,7 @@ async def create(
889884
codecs=codecs,
890885
dimension_names=dimension_names,
891886
attributes=attributes,
887+
order=order,
892888
**kwargs,
893889
)
894890

src/zarr/core/array.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,6 @@ async def create(
472472
raise ValueError(
473473
"dimension_separator cannot be used for arrays with version 3. Use chunk_key_encoding instead."
474474
)
475-
if order is not None:
476-
raise ValueError(
477-
"order cannot be used for arrays with version 3. Use a transpose codec instead."
478-
)
479475
if filters is not None:
480476
raise ValueError(
481477
"filters cannot be used for arrays with version 3. Use array-to-array codecs instead."
@@ -495,6 +491,7 @@ async def create(
495491
dimension_names=dimension_names,
496492
attributes=attributes,
497493
exists_ok=exists_ok,
494+
order=order,
498495
)
499496
elif zarr_format == 2:
500497
if dtype is str or dtype == "str":
@@ -546,6 +543,7 @@ async def _create_v3(
546543
dtype: npt.DTypeLike,
547544
chunk_shape: ChunkCoords,
548545
fill_value: Any | None = None,
546+
order: Literal["C", "F"] | None = None,
549547
chunk_key_encoding: (
550548
ChunkKeyEncoding
551549
| tuple[Literal["default"], Literal[".", "/"]]
@@ -589,7 +587,7 @@ async def _create_v3(
589587
attributes=attributes or {},
590588
)
591589

592-
array = cls(metadata=metadata, store_path=store_path)
590+
array = cls(metadata=metadata, store_path=store_path, order=order)
593591
await array._save_metadata(metadata, ensure_parents=True)
594592
return array
595593

@@ -612,7 +610,7 @@ async def _create_v2(
612610
if not exists_ok:
613611
await ensure_no_existing_node(store_path, zarr_format=2)
614612
if order is None:
615-
order = "C"
613+
order = config.get("array.order", "C")
616614

617615
if dimension_separator is None:
618616
dimension_separator = "."
@@ -628,7 +626,7 @@ async def _create_v2(
628626
filters=filters,
629627
attributes=attributes,
630628
)
631-
array = cls(metadata=metadata, store_path=store_path)
629+
array = cls(metadata=metadata, store_path=store_path, order=order)
632630
await array._save_metadata(metadata, ensure_parents=True)
633631
return array
634632

tests/test_api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ def test_open_with_mode_w_minus(tmp_path: pathlib.Path) -> None:
206206
zarr.open(store=tmp_path, mode="w-")
207207

208208

209+
@pytest.mark.parametrize("order", ["C", "F", None])
210+
@pytest.mark.parametrize("zarr_format", [2, 3])
211+
def test_array_order(order: str | None, zarr_format: int) -> None:
212+
arr = zarr.ones(shape=(2, 2), order=order, zarr_format=zarr_format)
213+
expected = order or zarr.config.get("array.order")
214+
assert arr.order == expected
215+
216+
vals = np.asarray(arr)
217+
if expected == "C":
218+
assert vals.flags.c_contiguous
219+
elif expected == "F":
220+
assert vals.flags.f_contiguous
221+
else:
222+
raise AssertionError
223+
224+
209225
# def test_lazy_loader():
210226
# foo = np.arange(100)
211227
# bar = np.arange(100, 0, -1)

tests/test_array.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,20 @@ def test_update_attrs(zarr_format: int) -> None:
417417

418418
arr2 = zarr.open_array(store=store, zarr_format=zarr_format)
419419
assert arr2.attrs["foo"] == "bar"
420+
421+
422+
@pytest.mark.parametrize("order", ["C", "F", None])
423+
@pytest.mark.parametrize("zarr_format", [2, 3])
424+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
425+
def test_array_create_order(order: str | None, zarr_format: int, store: MemoryStore) -> None:
426+
arr = Array.create(store=store, shape=(2, 2), order=order, zarr_format=zarr_format, dtype="i4")
427+
expected = order or zarr.config.get("array.order")
428+
assert arr.order == expected
429+
430+
vals = np.asarray(arr)
431+
if expected == "C":
432+
assert vals.flags.c_contiguous
433+
elif expected == "F":
434+
assert vals.flags.f_contiguous
435+
else:
436+
raise AssertionError

0 commit comments

Comments
 (0)