Skip to content

Commit 0465c2b

Browse files
authored
Fix typing errors in testing.stateful (#3045)
* Fix typing errors in testing.stateful * Add a dimensionnames type * Add bugfix entry * Fix properties test
1 parent 2609748 commit 0465c2b

File tree

12 files changed

+94
-66
lines changed

12 files changed

+94
-66
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ repos:
3737
- obstore>=0.5.1
3838
# Tests
3939
- pytest
40+
- hypothesis
4041
- repo: https://github.com/scientific-python/cookie
4142
rev: 2025.01.22
4243
hooks:

changes/3045.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the typing of ``dimension_names`` arguments throughout so that it now accepts iterables that contain `None` alongside `str`.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
356356
[[tool.mypy.overrides]]
357357
module = [
358358
"tests.package_with_entrypoint.*",
359+
"zarr.testing.stateful",
359360
"tests.test_codecs.test_transpose",
360361
"tests.test_config"
361362
]
@@ -365,7 +366,6 @@ strict = false
365366
# and fix the errors
366367
[[tool.mypy.overrides]]
367368
module = [
368-
"zarr.testing.stateful", # lots of hypothesis decorator errors
369369
"tests.test_codecs.test_codecs",
370370
"tests.test_metadata.*",
371371
"tests.test_store.*",

src/zarr/api/asynchronous.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
JSON,
1717
AccessModeLiteral,
1818
ChunkCoords,
19+
DimensionNames,
1920
MemoryOrder,
2021
ZarrFormat,
2122
_default_zarr_format,
@@ -865,7 +866,7 @@ async def create(
865866
| None
866867
) = None,
867868
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
868-
dimension_names: Iterable[str] | None = None,
869+
dimension_names: DimensionNames = None,
869870
storage_options: dict[str, Any] | None = None,
870871
config: ArrayConfigLike | None = None,
871872
**kwargs: Any,

src/zarr/api/synchronous.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
JSON,
3434
AccessModeLiteral,
3535
ChunkCoords,
36+
DimensionNames,
3637
MemoryOrder,
3738
ShapeLike,
3839
ZarrFormat,
@@ -626,7 +627,7 @@ def create(
626627
| None
627628
) = None,
628629
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
629-
dimension_names: Iterable[str] | None = None,
630+
dimension_names: DimensionNames = None,
630631
storage_options: dict[str, Any] | None = None,
631632
config: ArrayConfigLike | None = None,
632633
**kwargs: Any,
@@ -761,7 +762,7 @@ def create_array(
761762
zarr_format: ZarrFormat | None = 3,
762763
attributes: dict[str, JSON] | None = None,
763764
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
764-
dimension_names: Iterable[str] | None = None,
765+
dimension_names: DimensionNames = None,
765766
storage_options: dict[str, Any] | None = None,
766767
overwrite: bool = False,
767768
config: ArrayConfigLike | None = None,
@@ -926,7 +927,7 @@ def from_array(
926927
zarr_format: ZarrFormat | None = None,
927928
attributes: dict[str, JSON] | None = None,
928929
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
929-
dimension_names: Iterable[str] | None = None,
930+
dimension_names: DimensionNames = None,
930931
storage_options: dict[str, Any] | None = None,
931932
overwrite: bool = False,
932933
config: ArrayConfigLike | None = None,

src/zarr/core/array.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
ZARRAY_JSON,
5555
ZATTRS_JSON,
5656
ChunkCoords,
57+
DimensionNames,
5758
MemoryOrder,
5859
ShapeLike,
5960
ZarrFormat,
@@ -330,7 +331,7 @@ async def create(
330331
| None
331332
) = None,
332333
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
333-
dimension_names: Iterable[str] | None = None,
334+
dimension_names: DimensionNames = None,
334335
# runtime
335336
overwrite: bool = False,
336337
data: npt.ArrayLike | None = None,
@@ -358,7 +359,7 @@ async def create(
358359
| None
359360
) = None,
360361
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
361-
dimension_names: Iterable[str] | None = None,
362+
dimension_names: DimensionNames = None,
362363
# runtime
363364
overwrite: bool = False,
364365
data: npt.ArrayLike | None = None,
@@ -386,7 +387,7 @@ async def create(
386387
| None
387388
) = None,
388389
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
389-
dimension_names: Iterable[str] | None = None,
390+
dimension_names: DimensionNames = None,
390391
# v2 only
391392
chunks: ShapeLike | None = None,
392393
dimension_separator: Literal[".", "/"] | None = None,
@@ -421,7 +422,7 @@ async def create(
421422
| None
422423
) = None,
423424
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
424-
dimension_names: Iterable[str] | None = None,
425+
dimension_names: DimensionNames = None,
425426
# v2 only
426427
chunks: ShapeLike | None = None,
427428
dimension_separator: Literal[".", "/"] | None = None,
@@ -473,7 +474,7 @@ async def create(
473474
474475
These defaults can be changed by modifying the value of ``array.v3_default_filters``,
475476
``array.v3_default_serializer`` and ``array.v3_default_compressors`` in :mod:`zarr.core.config`.
476-
dimension_names : Iterable[str], optional
477+
dimension_names : Iterable[str | None], optional
477478
The names of the dimensions (default is None).
478479
Zarr format 3 only. Zarr format 2 arrays should not use this parameter.
479480
chunks : ShapeLike, optional
@@ -562,7 +563,7 @@ async def _create(
562563
| None
563564
) = None,
564565
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
565-
dimension_names: Iterable[str] | None = None,
566+
dimension_names: DimensionNames = None,
566567
# v2 only
567568
chunks: ShapeLike | None = None,
568569
dimension_separator: Literal[".", "/"] | None = None,
@@ -672,7 +673,7 @@ def _create_metadata_v3(
672673
fill_value: Any | None = None,
673674
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
674675
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
675-
dimension_names: Iterable[str] | None = None,
676+
dimension_names: DimensionNames = None,
676677
attributes: dict[str, JSON] | None = None,
677678
) -> ArrayV3Metadata:
678679
"""
@@ -723,7 +724,7 @@ async def _create_v3(
723724
| None
724725
) = None,
725726
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
726-
dimension_names: Iterable[str] | None = None,
727+
dimension_names: DimensionNames = None,
727728
attributes: dict[str, JSON] | None = None,
728729
overwrite: bool = False,
729730
) -> AsyncArray[ArrayV3Metadata]:
@@ -1743,7 +1744,7 @@ def create(
17431744
| None
17441745
) = None,
17451746
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
1746-
dimension_names: Iterable[str] | None = None,
1747+
dimension_names: DimensionNames = None,
17471748
# v2 only
17481749
chunks: ChunkCoords | None = None,
17491750
dimension_separator: Literal[".", "/"] | None = None,
@@ -1788,7 +1789,7 @@ def create(
17881789
17891790
These defaults can be changed by modifying the value of ``array.v3_default_filters``,
17901791
``array.v3_default_serializer`` and ``array.v3_default_compressors`` in :mod:`zarr.core.config`.
1791-
dimension_names : Iterable[str], optional
1792+
dimension_names : Iterable[str | None], optional
17921793
The names of the dimensions (default is None).
17931794
Zarr format 3 only. Zarr format 2 arrays should not use this parameter.
17941795
chunks : ChunkCoords, optional
@@ -1872,7 +1873,7 @@ def _create(
18721873
| None
18731874
) = None,
18741875
codecs: Iterable[Codec | dict[str, JSON]] | None = None,
1875-
dimension_names: Iterable[str] | None = None,
1876+
dimension_names: DimensionNames = None,
18761877
# v2 only
18771878
chunks: ChunkCoords | None = None,
18781879
dimension_separator: Literal[".", "/"] | None = None,
@@ -3821,7 +3822,7 @@ async def from_array(
38213822
zarr_format: ZarrFormat | None = None,
38223823
attributes: dict[str, JSON] | None = None,
38233824
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
3824-
dimension_names: Iterable[str] | None = None,
3825+
dimension_names: DimensionNames = None,
38253826
storage_options: dict[str, Any] | None = None,
38263827
overwrite: bool = False,
38273828
config: ArrayConfig | ArrayConfigLike | None = None,
@@ -3929,7 +3930,7 @@ async def from_array(
39293930
For Zarr format 2, the default is ``{"name": "v2", "separator": "."}}``.
39303931
If not specified and the data array has the same zarr format as the target array,
39313932
the chunk key encoding of the data array is used.
3932-
dimension_names : Iterable[str], optional
3933+
dimension_names : Iterable[str | None], optional
39333934
The names of the dimensions (default is None).
39343935
Zarr format 3 only. Zarr format 2 arrays should not use this parameter.
39353936
If not specified, defaults to the dimension names of the data array.
@@ -4083,7 +4084,7 @@ async def init_array(
40834084
zarr_format: ZarrFormat | None = 3,
40844085
attributes: dict[str, JSON] | None = None,
40854086
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
4086-
dimension_names: Iterable[str] | None = None,
4087+
dimension_names: DimensionNames = None,
40874088
overwrite: bool = False,
40884089
config: ArrayConfigLike | None,
40894090
) -> AsyncArray[ArrayV3Metadata] | AsyncArray[ArrayV2Metadata]:
@@ -4298,7 +4299,7 @@ async def create_array(
42984299
zarr_format: ZarrFormat | None = 3,
42994300
attributes: dict[str, JSON] | None = None,
43004301
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
4301-
dimension_names: Iterable[str] | None = None,
4302+
dimension_names: DimensionNames = None,
43024303
storage_options: dict[str, Any] | None = None,
43034304
overwrite: bool = False,
43044305
config: ArrayConfigLike | None = None,
@@ -4477,7 +4478,7 @@ def _parse_keep_array_attr(
44774478
order: MemoryOrder | None,
44784479
zarr_format: ZarrFormat | None,
44794480
chunk_key_encoding: ChunkKeyEncodingLike | None,
4480-
dimension_names: Iterable[str] | None,
4481+
dimension_names: DimensionNames,
44814482
) -> tuple[
44824483
ChunkCoords | Literal["auto"],
44834484
ShardsLike | None,
@@ -4488,7 +4489,7 @@ def _parse_keep_array_attr(
44884489
MemoryOrder | None,
44894490
ZarrFormat,
44904491
ChunkKeyEncodingLike | None,
4491-
Iterable[str] | None,
4492+
DimensionNames,
44924493
]:
44934494
if isinstance(data, Array):
44944495
if chunks == "keep":

src/zarr/core/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
JSON = str | int | float | Mapping[str, "JSON"] | Sequence["JSON"] | None
4141
MemoryOrder = Literal["C", "F"]
4242
AccessModeLiteral = Literal["r", "r+", "a", "w", "w-"]
43+
DimensionNames = Iterable[str | None] | None
4344

4445

4546
def product(tup: ChunkCoords) -> int:

src/zarr/core/group.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
ZGROUP_JSON,
4444
ZMETADATA_V2_JSON,
4545
ChunkCoords,
46+
DimensionNames,
4647
NodeType,
4748
ShapeLike,
4849
ZarrFormat,
@@ -1006,7 +1007,7 @@ async def create_array(
10061007
order: MemoryOrder | None = None,
10071008
attributes: dict[str, JSON] | None = None,
10081009
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
1009-
dimension_names: Iterable[str] | None = None,
1010+
dimension_names: DimensionNames = None,
10101011
storage_options: dict[str, Any] | None = None,
10111012
overwrite: bool = False,
10121013
config: ArrayConfig | ArrayConfigLike | None = None,
@@ -2381,7 +2382,7 @@ def create_array(
23812382
order: MemoryOrder | None = "C",
23822383
attributes: dict[str, JSON] | None = None,
23832384
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
2384-
dimension_names: Iterable[str] | None = None,
2385+
dimension_names: DimensionNames = None,
23852386
storage_options: dict[str, Any] | None = None,
23862387
overwrite: bool = False,
23872388
config: ArrayConfig | ArrayConfigLike | None = None,
@@ -2775,7 +2776,7 @@ def array(
27752776
order: MemoryOrder | None = "C",
27762777
attributes: dict[str, JSON] | None = None,
27772778
chunk_key_encoding: ChunkKeyEncodingLike | None = None,
2778-
dimension_names: Iterable[str] | None = None,
2779+
dimension_names: DimensionNames = None,
27792780
storage_options: dict[str, Any] | None = None,
27802781
overwrite: bool = False,
27812782
config: ArrayConfig | ArrayConfigLike | None = None,

src/zarr/core/metadata/v3.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
JSON,
3333
ZARR_JSON,
3434
ChunkCoords,
35+
DimensionNames,
3536
parse_named_configuration,
3637
parse_shapelike,
3738
)
@@ -242,7 +243,7 @@ class ArrayV3Metadata(Metadata):
242243
fill_value: Any
243244
codecs: tuple[Codec, ...]
244245
attributes: dict[str, Any] = field(default_factory=dict)
245-
dimension_names: tuple[str, ...] | None = None
246+
dimension_names: tuple[str | None, ...] | None = None
246247
zarr_format: Literal[3] = field(default=3, init=False)
247248
node_type: Literal["array"] = field(default="array", init=False)
248249
storage_transformers: tuple[dict[str, JSON], ...]
@@ -257,7 +258,7 @@ def __init__(
257258
fill_value: Any,
258259
codecs: Iterable[Codec | dict[str, JSON]],
259260
attributes: dict[str, JSON] | None,
260-
dimension_names: Iterable[str] | None,
261+
dimension_names: DimensionNames,
261262
storage_transformers: Iterable[dict[str, JSON]] | None = None,
262263
) -> None:
263264
"""

src/zarr/testing/stateful.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ def init_store(self) -> None:
326326
self.store.clear()
327327

328328
@rule(key=zarr_keys(), data=st.binary(min_size=0, max_size=MAX_BINARY_SIZE))
329-
def set(self, key: str, data: DataObject) -> None:
330-
note(f"(set) Setting {key!r} with {data}")
329+
def set(self, key: str, data: bytes) -> None:
330+
note(f"(set) Setting {key!r} with {data!r}")
331331
assert not self.store.read_only
332332
data_buf = cpu.Buffer.from_bytes(data)
333333
self.store.set(key, data_buf)

0 commit comments

Comments
 (0)