Skip to content

Commit 72622fd

Browse files
committed
add missing docstring and re-parametrize test
1 parent 120db2a commit 72622fd

File tree

2 files changed

+201
-25
lines changed

2 files changed

+201
-25
lines changed

src/zarr/core/group.py

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,9 +2439,149 @@ def require_groups(self, *names: str) -> tuple[Group, ...]:
24392439
"""
24402440
return tuple(map(Group, self._sync(self._async_group.require_groups(*names))))
24412441

2442-
def create(self, *args: Any, **kwargs: Any) -> Array:
2443-
# Backwards compatibility for 2.x
2444-
return self.create_array(*args, **kwargs)
2442+
def create(
2443+
self,
2444+
name: str,
2445+
*,
2446+
shape: ShapeLike | None = None,
2447+
dtype: ZDTypeLike | None = None,
2448+
data: np.ndarray[Any, np.dtype[Any]] | None = None,
2449+
chunks: tuple[int, ...] | Literal["auto"] = "auto",
2450+
shards: ShardsLike | None = None,
2451+
filters: FiltersLike = "auto",
2452+
compressors: CompressorsLike = "auto",
2453+
compressor: CompressorLike = "auto",
2454+
serializer: SerializerLike = "auto",
2455+
fill_value: Any | None = DEFAULT_FILL_VALUE,
2456+
order: MemoryOrder | None = None,
2457+
attributes: dict[str, JSON] | None = None,
2458+
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
2459+
dimension_names: DimensionNames = None,
2460+
storage_options: dict[str, Any] | None = None,
2461+
overwrite: bool = False,
2462+
config: ArrayConfigLike | None = None,
2463+
write_data: bool = True,
2464+
) -> Array:
2465+
"""Create an array within this group.
2466+
2467+
This method lightly wraps :func:`zarr.core.array.create_array`.
2468+
2469+
Parameters
2470+
----------
2471+
name : str
2472+
The name of the array relative to the group. If ``path`` is ``None``, the array will be located
2473+
at the root of the store.
2474+
shape : ShapeLike, optional
2475+
Shape of the array. Must be ``None`` if ``data`` is provided.
2476+
dtype : npt.DTypeLike | None
2477+
Data type of the array. Must be ``None`` if ``data`` is provided.
2478+
data : Array-like data to use for initializing the array. If this parameter is provided, the
2479+
``shape`` and ``dtype`` parameters must be ``None``.
2480+
chunks : tuple[int, ...], optional
2481+
Chunk shape of the array.
2482+
If not specified, default are guessed based on the shape and dtype.
2483+
shards : tuple[int, ...], optional
2484+
Shard shape of the array. The default value of ``None`` results in no sharding at all.
2485+
filters : Iterable[Codec] | Literal["auto"], optional
2486+
Iterable of filters to apply to each chunk of the array, in order, before serializing that
2487+
chunk to bytes.
2488+
2489+
For Zarr format 3, a "filter" is a codec that takes an array and returns an array,
2490+
and these values must be instances of :class:`zarr.abc.codec.ArrayArrayCodec`, or a
2491+
dict representations of :class:`zarr.abc.codec.ArrayArrayCodec`.
2492+
2493+
For Zarr format 2, a "filter" can be any numcodecs codec; you should ensure that the
2494+
the order if your filters is consistent with the behavior of each filter.
2495+
2496+
The default value of ``"auto"`` instructs Zarr to use a default used based on the data
2497+
type of the array and the Zarr format specified. For all data types in Zarr V3, and most
2498+
data types in Zarr V2, the default filters are empty. The only cases where default filters
2499+
are not empty is when the Zarr format is 2, and the data type is a variable-length data type like
2500+
`:class:zarr.dtype.VariableLengthUTF8` or `:class:zarr.dtype.VariableLengthUTF8`. In these cases,
2501+
the default filters contains a single element which is a codec specific to that particular data type.
2502+
2503+
To create an array with no filters, provide an empty iterable or the value ``None``.
2504+
compressors : Iterable[Codec], optional
2505+
List of compressors to apply to the array. Compressors are applied in order, and after any
2506+
filters are applied (if any are specified) and the data is serialized into bytes.
2507+
2508+
For Zarr format 3, a "compressor" is a codec that takes a bytestream, and
2509+
returns another bytestream. Multiple compressors my be provided for Zarr format 3.
2510+
If no ``compressors`` are provided, a default set of compressors will be used.
2511+
These defaults can be changed by modifying the value of ``array.v3_default_compressors``
2512+
in :mod:`zarr.core.config`.
2513+
Use ``None`` to omit default compressors.
2514+
2515+
For Zarr format 2, a "compressor" can be any numcodecs codec. Only a single compressor may
2516+
be provided for Zarr format 2.
2517+
If no ``compressor`` is provided, a default compressor will be used.
2518+
in :mod:`zarr.core.config`.
2519+
Use ``None`` to omit the default compressor.
2520+
compressor : Codec, optional
2521+
Deprecated in favor of ``compressors``.
2522+
serializer : dict[str, JSON] | ArrayBytesCodec, optional
2523+
Array-to-bytes codec to use for encoding the array data.
2524+
Zarr format 3 only. Zarr format 2 arrays use implicit array-to-bytes conversion.
2525+
If no ``serializer`` is provided, a default serializer will be used.
2526+
These defaults can be changed by modifying the value of ``array.v3_default_serializer``
2527+
in :mod:`zarr.core.config`.
2528+
fill_value : Any, optional
2529+
Fill value for the array.
2530+
order : {"C", "F"}, optional
2531+
The memory of the array (default is "C").
2532+
For Zarr format 2, this parameter sets the memory order of the array.
2533+
For Zarr format 3, this parameter is deprecated, because memory order
2534+
is a runtime parameter for Zarr format 3 arrays. The recommended way to specify the memory
2535+
order for Zarr format 3 arrays is via the ``config`` parameter, e.g. ``{'config': 'C'}``.
2536+
If no ``order`` is provided, a default order will be used.
2537+
This default can be changed by modifying the value of ``array.order`` in :mod:`zarr.core.config`.
2538+
attributes : dict, optional
2539+
Attributes for the array.
2540+
chunk_key_encoding : ChunkKeyEncoding, optional
2541+
A specification of how the chunk keys are represented in storage.
2542+
For Zarr format 3, the default is ``{"name": "default", "separator": "/"}}``.
2543+
For Zarr format 2, the default is ``{"name": "v2", "separator": "."}}``.
2544+
dimension_names : Iterable[str], optional
2545+
The names of the dimensions (default is None).
2546+
Zarr format 3 only. Zarr format 2 arrays should not use this parameter.
2547+
storage_options : dict, optional
2548+
If using an fsspec URL to create the store, these will be passed to the backend implementation.
2549+
Ignored otherwise.
2550+
overwrite : bool, default False
2551+
Whether to overwrite an array with the same name in the store, if one exists.
2552+
config : ArrayConfig or ArrayConfigLike, optional
2553+
Runtime configuration for the array.
2554+
write_data : bool
2555+
If a pre-existing array-like object was provided to this function via the ``data`` parameter
2556+
then ``write_data`` determines whether the values in that array-like object should be
2557+
written to the Zarr array created by this function. If ``write_data`` is ``False``, then the
2558+
array will be left empty.
2559+
2560+
Returns
2561+
-------
2562+
AsyncArray
2563+
"""
2564+
return self.create_array(
2565+
name,
2566+
shape=shape,
2567+
dtype=dtype,
2568+
data=data,
2569+
chunks=chunks,
2570+
shards=shards,
2571+
filters=filters,
2572+
compressors=compressors,
2573+
compressor=compressor,
2574+
serializer=serializer,
2575+
fill_value=fill_value,
2576+
order=order,
2577+
attributes=attributes,
2578+
chunk_key_encoding=chunk_key_encoding,
2579+
dimension_names=dimension_names,
2580+
storage_options=storage_options,
2581+
overwrite=overwrite,
2582+
config=config,
2583+
write_data=write_data,
2584+
)
24452585

24462586
def create_array(
24472587
self,

tests/test_api/test_synchronous.py

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from numpydoc.docscrape import NumpyDocString
77

8+
import zarr
89
from zarr.api import asynchronous, synchronous
910

1011
if TYPE_CHECKING:
@@ -37,37 +38,73 @@ def test_docstring_match(callable_name: str) -> None:
3738

3839

3940
@pytest.mark.parametrize(
40-
"parameter_name",
41-
[
42-
"store",
43-
"path",
44-
"filters",
45-
"codecs",
46-
"compressors",
47-
"compressor",
48-
"chunks",
49-
"shape",
50-
"dtype",
51-
"fill_value",
52-
],
53-
)
54-
@pytest.mark.parametrize(
55-
"array_creation_routines",
41+
("parameter_name", "array_creation_routines"),
5642
[
5743
(
58-
asynchronous.create_array,
59-
synchronous.create_array,
60-
asynchronous.create_group,
61-
synchronous.create_group,
44+
("store", "path"),
45+
(
46+
asynchronous.create_array,
47+
synchronous.create_array,
48+
asynchronous.create_group,
49+
synchronous.create_group,
50+
zarr.AsyncGroup.create_array,
51+
zarr.Group.create_array,
52+
),
53+
),
54+
(
55+
(
56+
"store",
57+
"path",
58+
),
59+
(
60+
asynchronous.create,
61+
synchronous.create,
62+
zarr.Group.create,
63+
zarr.AsyncArray.create,
64+
zarr.Array.create,
65+
),
66+
),
67+
(
68+
(
69+
(
70+
"filters",
71+
"codecs",
72+
"compressors",
73+
"compressor",
74+
"chunks",
75+
"shape",
76+
"dtype",
77+
"shardsfill_value",
78+
)
79+
),
80+
(
81+
asynchronous.create,
82+
synchronous.create,
83+
asynchronous.create_array,
84+
synchronous.create_array,
85+
zarr.AsyncGroup.create_array,
86+
zarr.Group.create_array,
87+
zarr.AsyncGroup.create_dataset,
88+
zarr.Group.create_dataset,
89+
),
6290
),
63-
(asynchronous.create, synchronous.create),
6491
],
92+
ids=str,
6593
)
6694
def test_docstring_consistent_parameters(
6795
parameter_name: str, array_creation_routines: tuple[Callable[[Any], Any], ...]
6896
) -> None:
6997
"""
7098
Tests that array and group creation routines document the same parameters consistently.
99+
This test inspecs the docstrings of sets of callables and generates two dicts:
100+
101+
- a dict where the keys are parameter descriptions and the values are the names of the routines with those
102+
descriptions
103+
- a dict where the keys are parameter types and the values are the names of the routines with those types
104+
105+
If each dict has just 1 value, then the parameter description and type in the docstring must be
106+
identical across different routines. But if these dicts have multiple values, then there must be
107+
routines that use the same parameter but document it differently, which will trigger a test failure.
71108
"""
72109
descs: dict[tuple[str, ...], tuple[str, ...]] = {}
73110
types: dict[str, tuple[str, ...]] = {}
@@ -85,6 +122,5 @@ def test_docstring_consistent_parameters(
85122
types[val.type] = types[val.type] + (key,)
86123
else:
87124
types[val.type] = (key,)
88-
89125
assert len(descs) <= 1
90126
assert len(types) <= 1

0 commit comments

Comments
 (0)