Skip to content

Commit 64ad525

Browse files
committed
fix(array): thread order parameter through to array __init__
1 parent 37fde7d commit 64ad525

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
@@ -471,10 +471,6 @@ async def create(
471471
raise ValueError(
472472
"dimension_separator cannot be used for arrays with version 3. Use chunk_key_encoding instead."
473473
)
474-
if order is not None:
475-
raise ValueError(
476-
"order cannot be used for arrays with version 3. Use a transpose codec instead."
477-
)
478474
if filters is not None:
479475
raise ValueError(
480476
"filters cannot be used for arrays with version 3. Use array-to-array codecs instead."
@@ -494,6 +490,7 @@ async def create(
494490
dimension_names=dimension_names,
495491
attributes=attributes,
496492
exists_ok=exists_ok,
493+
order=order,
497494
)
498495
elif zarr_format == 2:
499496
if dtype is str or dtype == "str":
@@ -545,6 +542,7 @@ async def _create_v3(
545542
dtype: npt.DTypeLike,
546543
chunk_shape: ChunkCoords,
547544
fill_value: Any | None = None,
545+
order: Literal["C", "F"] | None = None,
548546
chunk_key_encoding: (
549547
ChunkKeyEncoding
550548
| tuple[Literal["default"], Literal[".", "/"]]
@@ -588,7 +586,7 @@ async def _create_v3(
588586
attributes=attributes or {},
589587
)
590588

591-
array = cls(metadata=metadata, store_path=store_path)
589+
array = cls(metadata=metadata, store_path=store_path, order=order)
592590
await array._save_metadata(metadata, ensure_parents=True)
593591
return array
594592

@@ -611,7 +609,7 @@ async def _create_v2(
611609
if not exists_ok:
612610
await ensure_no_existing_node(store_path, zarr_format=2)
613611
if order is None:
614-
order = "C"
612+
order = config.get("array.order", "C")
615613

616614
if dimension_separator is None:
617615
dimension_separator = "."
@@ -627,7 +625,7 @@ async def _create_v2(
627625
filters=filters,
628626
attributes=attributes,
629627
)
630-
array = cls(metadata=metadata, store_path=store_path)
628+
array = cls(metadata=metadata, store_path=store_path, order=order)
631629
await array._save_metadata(metadata, ensure_parents=True)
632630
return array
633631

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)