Skip to content

Commit 668eac6

Browse files
authored
Merge branch 'main' into test-stateful-hierarchy
2 parents 059fe59 + 693e11c commit 668eac6

File tree

24 files changed

+296
-160
lines changed

24 files changed

+296
-160
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ jobs:
3333
numpy-version: '2.1'
3434
dependency-set: 'optional'
3535
os: 'macos-latest'
36-
# https://github.com/zarr-developers/zarr-python/issues/2438
37-
# - python-version: '3.11'
38-
# numpy-version: '1.25'
39-
# dependency-set: 'optional'
40-
# os: 'windows-latest'
41-
# - python-version: '3.13'
42-
# numpy-version: '2.1'
43-
# dependency-set: 'optional'
44-
# os: 'windows-latest'
36+
- python-version: '3.11'
37+
numpy-version: '1.25'
38+
dependency-set: 'optional'
39+
os: 'windows-latest'
40+
- python-version: '3.13'
41+
numpy-version: '2.1'
42+
dependency-set: 'optional'
43+
os: 'windows-latest'
4544
runs-on: ${{ matrix.os }}
4645

4746
steps:

pyproject.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,25 @@ extend-exclude = [
269269
extend-select = [
270270
"ANN", # flake8-annotations
271271
"B", # flake8-bugbear
272+
"EXE", # flake8-executable
272273
"C4", # flake8-comprehensions
274+
"FA", # flake8-future-annotations
273275
"FLY", # flynt
274276
"FURB", # refurb
275277
"G", # flake8-logging-format
276278
"I", # isort
277279
"ISC", # flake8-implicit-str-concat
280+
"LOG", # flake8-logging
278281
"PERF", # Perflint
282+
"PIE", # flake8-pie
279283
"PGH", # pygrep-hooks
280284
"PT", # flake8-pytest-style
281285
"PYI", # flake8-pyi
282-
"RSE", # flake8-raise
283286
"RET", # flake8-return
287+
"RSE", # flake8-raise
284288
"RUF",
289+
"SIM", # flake8-simplify
290+
"SLOT", # flake8-slots
285291
"TCH", # flake8-type-checking
286292
"TRY", # tryceratops
287293
"UP", # pyupgrade
@@ -298,6 +304,7 @@ ignore = [
298304
"RET505",
299305
"RET506",
300306
"RUF005",
307+
"SIM108",
301308
"TRY003",
302309
"UP027", # deprecated
303310
"UP038", # https://github.com/astral-sh/ruff/issues/7871
@@ -319,7 +326,7 @@ ignore = [
319326
]
320327

321328
[tool.ruff.lint.extend-per-file-ignores]
322-
"tests/**" = ["ANN001", "ANN201"]
329+
"tests/**" = ["ANN001", "ANN201", "RUF029", "SIM117", "SIM300"]
323330

324331
[tool.mypy]
325332
python_version = "3.11"

src/zarr/abc/codec.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def validate(self, *, shape: ChunkCoords, dtype: np.dtype[Any], chunk_grid: Chun
106106
chunk_grid : ChunkGrid
107107
The array chunk grid
108108
"""
109-
...
110109

111110
async def _decode_single(self, chunk_data: CodecOutput, chunk_spec: ArraySpec) -> CodecInput:
112111
raise NotImplementedError

src/zarr/abc/metadata.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,5 @@ def from_dict(cls, data: dict[str, JSON]) -> Self:
4242
"""
4343
Create an instance of the model from a dictionary
4444
"""
45-
...
4645

4746
return cls(**data)

src/zarr/abc/store.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import TYPE_CHECKING, NamedTuple, Protocol, runtime_checkable
77

88
if TYPE_CHECKING:
9-
from collections.abc import AsyncGenerator, Iterable
9+
from collections.abc import AsyncGenerator, AsyncIterator, Iterable
1010
from types import TracebackType
1111
from typing import Any, Self, TypeAlias
1212

@@ -329,17 +329,19 @@ def supports_listing(self) -> bool:
329329
...
330330

331331
@abstractmethod
332-
def list(self) -> AsyncGenerator[str, None]:
332+
def list(self) -> AsyncIterator[str]:
333333
"""Retrieve all keys in the store.
334334
335335
Returns
336336
-------
337-
AsyncGenerator[str, None]
337+
AsyncIterator[str]
338338
"""
339-
...
339+
# This method should be async, like overridden methods in child classes.
340+
# However, that's not straightforward:
341+
# https://stackoverflow.com/questions/68905848
340342

341343
@abstractmethod
342-
def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
344+
def list_prefix(self, prefix: str) -> AsyncIterator[str]:
343345
"""
344346
Retrieve all keys in the store that begin with a given prefix. Keys are returned relative
345347
to the root of the store.
@@ -350,12 +352,14 @@ def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
350352
351353
Returns
352354
-------
353-
AsyncGenerator[str, None]
355+
AsyncIterator[str]
354356
"""
355-
...
357+
# This method should be async, like overridden methods in child classes.
358+
# However, that's not straightforward:
359+
# https://stackoverflow.com/questions/68905848
356360

357361
@abstractmethod
358-
def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
362+
def list_dir(self, prefix: str) -> AsyncIterator[str]:
359363
"""
360364
Retrieve all keys and prefixes with a given prefix and which do not contain the character
361365
“/” after the given prefix.
@@ -366,9 +370,11 @@ def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
366370
367371
Returns
368372
-------
369-
AsyncGenerator[str, None]
373+
AsyncIterator[str]
370374
"""
371-
...
375+
# This method should be async, like overridden methods in child classes.
376+
# However, that's not straightforward:
377+
# https://stackoverflow.com/questions/68905848
372378

373379
async def delete_dir(self, prefix: str) -> None:
374380
"""

src/zarr/api/asynchronous.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,8 @@ async def create(
878878
warnings.warn("meta_array is not yet implemented", RuntimeWarning, stacklevel=2)
879879

880880
mode = kwargs.pop("mode", None)
881-
if mode is None:
882-
if not isinstance(store, Store | StorePath):
883-
mode = "a"
881+
if mode is None and not isinstance(store, Store | StorePath):
882+
mode = "a"
884883

885884
store_path = await make_store_path(store, path=path, mode=mode, storage_options=storage_options)
886885

src/zarr/codecs/gzip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
def parse_gzip_level(data: JSON) -> int:
2222
if not isinstance(data, (int)):
2323
raise TypeError(f"Expected int, got {type(data)}")
24-
if data not in range(0, 10):
24+
if data not in range(10):
2525
raise ValueError(
2626
f"Expected an integer from the inclusive range (0, 9). Got {data} instead."
2727
)

src/zarr/core/array.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,29 @@ def from_dict(
645645
store_path: StorePath,
646646
data: dict[str, JSON],
647647
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
648+
"""
649+
Create a Zarr array from a dictionary, with support for both Zarr v2 and v3 metadata.
650+
651+
Parameters
652+
----------
653+
store_path : StorePath
654+
The path within the store where the array should be created.
655+
656+
data : dict
657+
A dictionary representing the array data. This dictionary should include necessary metadata
658+
for the array, such as shape, dtype, and other attributes. The format of the metadata
659+
will determine whether a Zarr v2 or v3 array is created.
660+
661+
Returns
662+
-------
663+
AsyncArray[ArrayV3Metadata] or AsyncArray[ArrayV2Metadata]
664+
The created Zarr array, either using v2 or v3 metadata based on the provided data.
665+
666+
Raises
667+
------
668+
ValueError
669+
If the dictionary data is invalid or incompatible with either Zarr v2 or v3 array creation.
670+
"""
648671
metadata = parse_array_metadata(data)
649672
return cls(metadata=metadata, store_path=store_path)
650673

@@ -1040,6 +1063,9 @@ async def getitem(
10401063
return await self._get_selection(indexer, prototype=prototype)
10411064

10421065
async def _save_metadata(self, metadata: ArrayMetadata, ensure_parents: bool = False) -> None:
1066+
"""
1067+
Asynchronously save the array metadata.
1068+
"""
10431069
to_save = metadata.to_buffer_dict(default_buffer_prototype())
10441070
awaitables = [set_or_delete(self.store_path / key, value) for key, value in to_save.items()]
10451071

@@ -1118,6 +1144,39 @@ async def setitem(
11181144
value: npt.ArrayLike,
11191145
prototype: BufferPrototype | None = None,
11201146
) -> None:
1147+
"""
1148+
Asynchronously set values in the array using basic indexing.
1149+
1150+
Parameters
1151+
----------
1152+
selection : BasicSelection
1153+
The selection defining the region of the array to set.
1154+
1155+
value : numpy.typing.ArrayLike
1156+
The values to be written into the selected region of the array.
1157+
1158+
prototype : BufferPrototype or None, optional
1159+
A prototype buffer that defines the structure and properties of the array chunks being modified.
1160+
If None, the default buffer prototype is used. Default is None.
1161+
1162+
Returns
1163+
-------
1164+
None
1165+
This method does not return any value.
1166+
1167+
Raises
1168+
------
1169+
IndexError
1170+
If the selection is out of bounds for the array.
1171+
1172+
ValueError
1173+
If the values are not compatible with the array's dtype or shape.
1174+
1175+
Notes
1176+
-----
1177+
- This method is asynchronous and should be awaited.
1178+
- Supports basic indexing, where the selection is contiguous and does not involve advanced indexing.
1179+
"""
11211180
if prototype is None:
11221181
prototype = default_buffer_prototype()
11231182
indexer = BasicIndexer(
@@ -1128,6 +1187,32 @@ async def setitem(
11281187
return await self._set_selection(indexer, value, prototype=prototype)
11291188

11301189
async def resize(self, new_shape: ShapeLike, delete_outside_chunks: bool = True) -> None:
1190+
"""
1191+
Asynchronously resize the array to a new shape.
1192+
1193+
Parameters
1194+
----------
1195+
new_shape : ChunkCoords
1196+
The desired new shape of the array.
1197+
1198+
delete_outside_chunks : bool, optional
1199+
If True (default), chunks that fall outside the new shape will be deleted. If False,
1200+
the data in those chunks will be preserved.
1201+
1202+
Returns
1203+
-------
1204+
AsyncArray
1205+
The resized array.
1206+
1207+
Raises
1208+
------
1209+
ValueError
1210+
If the new shape is incompatible with the current array's chunking configuration.
1211+
1212+
Notes
1213+
-----
1214+
- This method is asynchronous and should be awaited.
1215+
"""
11311216
new_shape = parse_shapelike(new_shape)
11321217
assert len(new_shape) == len(self.metadata.shape)
11331218
new_metadata = self.metadata.update_shape(new_shape)
@@ -1210,6 +1295,31 @@ async def append(self, data: npt.ArrayLike, axis: int = 0) -> ChunkCoords:
12101295
return new_shape
12111296

12121297
async def update_attributes(self, new_attributes: dict[str, JSON]) -> Self:
1298+
"""
1299+
Asynchronously update the array's attributes.
1300+
1301+
Parameters
1302+
----------
1303+
new_attributes : dict of str to JSON
1304+
A dictionary of new attributes to update or add to the array. The keys represent attribute
1305+
names, and the values must be JSON-compatible.
1306+
1307+
Returns
1308+
-------
1309+
AsyncArray
1310+
The array with the updated attributes.
1311+
1312+
Raises
1313+
------
1314+
ValueError
1315+
If the attributes are invalid or incompatible with the array's metadata.
1316+
1317+
Notes
1318+
-----
1319+
- This method is asynchronous and should be awaited.
1320+
- The updated attributes will be merged with existing attributes, and any conflicts will be
1321+
overwritten by the new values.
1322+
"""
12131323
# metadata.attributes is "frozen" so we simply clear and update the dict
12141324
self.metadata.attributes.clear()
12151325
self.metadata.attributes.update(new_attributes)
@@ -1328,6 +1438,28 @@ def from_dict(
13281438
store_path: StorePath,
13291439
data: dict[str, JSON],
13301440
) -> Array:
1441+
"""
1442+
Create a Zarr array from a dictionary.
1443+
1444+
Parameters
1445+
----------
1446+
store_path : StorePath
1447+
The path within the store where the array should be created.
1448+
1449+
data : dict
1450+
A dictionary representing the array data. This dictionary should include necessary metadata
1451+
for the array, such as shape, dtype, fill value, and attributes.
1452+
1453+
Returns
1454+
-------
1455+
Array
1456+
The created Zarr array.
1457+
1458+
Raises
1459+
------
1460+
ValueError
1461+
If the dictionary data is invalid or missing required fields for array creation.
1462+
"""
13311463
async_array = AsyncArray.from_dict(store_path=store_path, data=data)
13321464
return cls(async_array)
13331465

@@ -2934,6 +3066,30 @@ def append(self, data: npt.ArrayLike, axis: int = 0) -> ChunkCoords:
29343066
return sync(self._async_array.append(data, axis=axis))
29353067

29363068
def update_attributes(self, new_attributes: dict[str, JSON]) -> Array:
3069+
"""
3070+
Update the array's attributes.
3071+
3072+
Parameters
3073+
----------
3074+
new_attributes : dict
3075+
A dictionary of new attributes to update or add to the array. The keys represent attribute
3076+
names, and the values must be JSON-compatible.
3077+
3078+
Returns
3079+
-------
3080+
Array
3081+
The array with the updated attributes.
3082+
3083+
Raises
3084+
------
3085+
ValueError
3086+
If the attributes are invalid or incompatible with the array's metadata.
3087+
3088+
Notes
3089+
-----
3090+
- The updated attributes will be merged with existing attributes, and any conflicts will be
3091+
overwritten by the new values.
3092+
"""
29373093
# TODO: remove this cast when type inference improves
29383094
new_array = sync(self._async_array.update_attributes(new_attributes))
29393095
# TODO: remove this cast when type inference improves

src/zarr/core/chunk_grids.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def to_dict(self) -> dict[str, JSON]:
182182

183183
def all_chunk_coords(self, array_shape: ChunkCoords) -> Iterator[ChunkCoords]:
184184
return itertools.product(
185-
*(range(0, ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape, strict=False))
185+
*(range(ceildiv(s, c)) for s, c in zip(array_shape, self.chunk_shape, strict=False))
186186
)
187187

188188
def get_nchunks(self, array_shape: ChunkCoords) -> int:

0 commit comments

Comments
 (0)