Skip to content

Commit 4c966c0

Browse files
committed
test for consistent signatures, and make array default fill value consistently 0
1 parent 210f4e9 commit 4c966c0

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

src/zarr/api/synchronous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ def create_array(
753753
filters: FiltersLike = "auto",
754754
compressors: CompressorsLike = "auto",
755755
serializer: SerializerLike = "auto",
756-
fill_value: Any | None = None,
756+
fill_value: Any | None = 0,
757757
order: MemoryOrder | None = None,
758758
zarr_format: ZarrFormat | None = 3,
759759
attributes: dict[str, JSON] | None = None,

src/zarr/core/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4015,7 +4015,7 @@ async def create_array(
40154015
filters: FiltersLike = "auto",
40164016
compressors: CompressorsLike = "auto",
40174017
serializer: SerializerLike = "auto",
4018-
fill_value: Any | None = None,
4018+
fill_value: Any | None = 0,
40194019
order: MemoryOrder | None = None,
40204020
zarr_format: ZarrFormat | None = 3,
40214021
attributes: dict[str, JSON] | None = None,

src/zarr/core/group.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,8 +1005,9 @@ async def create_array(
10051005
self,
10061006
name: str,
10071007
*,
1008-
shape: ShapeLike,
1009-
dtype: npt.DTypeLike,
1008+
shape: ShapeLike | None = None,
1009+
dtype: npt.DTypeLike | None = None,
1010+
data: np.ndarray[Any, np.dtype[Any]] | None = None,
10101011
chunks: ChunkCoords | Literal["auto"] = "auto",
10111012
shards: ShardsLike | None = None,
10121013
filters: FiltersLike = "auto",
@@ -1016,11 +1017,12 @@ async def create_array(
10161017
fill_value: Any | None = 0,
10171018
order: MemoryOrder | None = None,
10181019
attributes: dict[str, JSON] | None = None,
1019-
chunk_key_encoding: ChunkKeyEncoding | ChunkKeyEncodingLike | None = None,
1020+
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
10201021
dimension_names: Iterable[str] | None = None,
10211022
storage_options: dict[str, Any] | None = None,
10221023
overwrite: bool = False,
10231024
config: ArrayConfig | ArrayConfigLike | None = None,
1025+
write_data: bool = True,
10241026
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
10251027
"""Create an array within this group.
10261028
@@ -1108,6 +1110,11 @@ async def create_array(
11081110
Whether to overwrite an array with the same name in the store, if one exists.
11091111
config : ArrayConfig or ArrayConfigLike, optional
11101112
Runtime configuration for the array.
1113+
write_data : bool
1114+
If a pre-existing array-like object was provided to this function via the ``data`` parameter
1115+
then ``write_data`` determines whether the values in that array-like object should be
1116+
written to the Zarr array created by this function. If ``write_data`` is ``False``, then the
1117+
array will be left empty.
11111118
11121119
Returns
11131120
-------
@@ -1122,6 +1129,7 @@ async def create_array(
11221129
name=name,
11231130
shape=shape,
11241131
dtype=dtype,
1132+
data=data,
11251133
chunks=chunks,
11261134
shards=shards,
11271135
filters=filters,
@@ -1136,6 +1144,7 @@ async def create_array(
11361144
storage_options=storage_options,
11371145
overwrite=overwrite,
11381146
config=config,
1147+
write_data=write_data,
11391148
)
11401149

11411150
@deprecated("Use AsyncGroup.create_array instead.")

tests/test_array.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dataclasses
2+
import inspect
23
import json
34
import math
45
import multiprocessing as mp
@@ -1021,7 +1022,7 @@ def test_chunks_and_shards() -> None:
10211022

10221023
def test_create_array_default_fill_values() -> None:
10231024
a = zarr.create_array(MemoryStore(), shape=(5,), chunks=(5,), dtype="<U4")
1024-
assert a.fill_value == ""
1025+
assert a.fill_value == "0"
10251026

10261027
b = zarr.create_array(MemoryStore(), shape=(5,), chunks=(5,), dtype="<S4")
10271028
assert b.fill_value == b""
@@ -1420,3 +1421,22 @@ def test_multiprocessing(store: Store, method: Literal["fork", "spawn", "forkser
14201421

14211422
results = pool.starmap(_index_array, [(arr, slice(len(data)))])
14221423
assert all(np.array_equal(r, data) for r in results)
1424+
1425+
1426+
def test_create_array_method_signature() -> None:
1427+
"""
1428+
Test that the signature of the ``AsyncGroup.create_array`` function has nearly the same signature
1429+
as the ``create_array`` function. ``AsyncGroup.create_array`` should take all of the same keyword
1430+
arguments as ``create_array`` except ``store``.
1431+
"""
1432+
1433+
base_sig = inspect.signature(create_array)
1434+
meth_sig = inspect.signature(AsyncGroup.create_array)
1435+
# ignore keyword arguments that are either missing or have different semantics when
1436+
# create_array is invoked as a group method
1437+
ignore_kwargs = {"zarr_format", "store", "name"}
1438+
# TODO: make this test stronger. right now, it only checks that all the parameters in the
1439+
# function signature are used in the method signature. we can be more strict and check that
1440+
# the method signature uses no extra parameters.
1441+
base_params = dict(filter(lambda kv: kv[0] not in ignore_kwargs, base_sig.parameters.items()))
1442+
assert (set(base_params.items()) - set(meth_sig.parameters.items())) == set()

0 commit comments

Comments
 (0)