Skip to content

Commit 65fec71

Browse files
Some minor linter \ type-hint fixes for my own sanity (#3623)
* ✏️ using ZarrFormat instead of duplication * ✏️ making linter happy (numpy has Literal[True, False]) * Create 3623.misc.md
1 parent ee0e69a commit 65fec71

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

changes/3623.misc.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This PR contains minor, non-function-altering, changes to use `ZarrFormat` across the repo as opposed to duplicating is with `Literal[2,3]`.
2+
3+
Additionally, it fixes broken linting by using a `Literal[True, False]` type hint for Numpy hypothesis testing, as opposed to `bool`.
4+
5+
Basically improves the typehints and reduces fat-finger error surface area slightly.

examples/custom_dtype/custom_dtype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def from_json_scalar(self, data: JSON, *, zarr_format: ZarrFormat) -> ml_dtypes.
217217

218218
# this parametrized function will create arrays in zarr v2 and v3 using our new data type
219219
@pytest.mark.parametrize("zarr_format", [2, 3])
220-
def test_custom_dtype(tmp_path: Path, zarr_format: Literal[2, 3]) -> None:
220+
def test_custom_dtype(tmp_path: Path, zarr_format: ZarrFormat) -> None:
221221
# create array and write values
222222
z_w = zarr.create_array(
223223
store=tmp_path, shape=(4,), dtype="int2", zarr_format=zarr_format, compressors=None

src/zarr/_cli/cli.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import zarr
88
import zarr.metadata.migrate_v3 as migrate_metadata
9+
from zarr.core.common import ZarrFormat
910
from zarr.core.sync import sync
1011
from zarr.storage._common import make_store
1112

@@ -23,12 +24,12 @@ def _set_logging_level(*, verbose: bool) -> None:
2324
zarr.set_format("%(message)s")
2425

2526

26-
class ZarrFormat(str, Enum):
27+
class CLIZarrFormat(str, Enum):
2728
v2 = "v2"
2829
v3 = "v3"
2930

3031

31-
class ZarrFormatV3(str, Enum):
32+
class CLIZarrFormatV3(str, Enum):
3233
"""Limit CLI choice to only v3"""
3334

3435
v3 = "v3"
@@ -37,7 +38,7 @@ class ZarrFormatV3(str, Enum):
3738
@app.command() # type: ignore[misc]
3839
def migrate(
3940
zarr_format: Annotated[
40-
ZarrFormatV3,
41+
CLIZarrFormatV3,
4142
typer.Argument(
4243
help="Zarr format to migrate to. Currently only 'v3' is supported.",
4344
),
@@ -122,7 +123,7 @@ def migrate(
122123
@app.command() # type: ignore[misc]
123124
def remove_metadata(
124125
zarr_format: Annotated[
125-
ZarrFormat,
126+
CLIZarrFormat,
126127
typer.Argument(help="Which format's metadata to remove - v2 or v3."),
127128
],
128129
store: Annotated[
@@ -160,7 +161,7 @@ def remove_metadata(
160161
sync(
161162
migrate_metadata.remove_metadata(
162163
store=input_zarr_store,
163-
zarr_format=cast(Literal[2, 3], int(zarr_format[1:])),
164+
zarr_format=cast(ZarrFormat, int(zarr_format[1:])),
164165
force=force,
165166
dry_run=dry_run,
166167
)

src/zarr/metadata/migrate_v3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import logging
3-
from typing import Literal, cast
3+
from typing import cast
44

55
import numcodecs.abc
66

@@ -140,7 +140,7 @@ async def remove_metadata(
140140
continue
141141

142142
if force or await _metadata_exists(
143-
cast(Literal[2, 3], alternative_metadata), store_path / parent_path
143+
cast(ZarrFormat, alternative_metadata), store_path / parent_path
144144
):
145145
_logger.info("Deleting metadata at %s", store_path / file_path)
146146
if not dry_run:

src/zarr/testing/strategies.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from zarr.storage._utils import normalize_path
2626
from zarr.types import AnyArray
2727

28+
TrueOrFalse = Literal[True, False]
29+
2830
# Copied from Xarray
2931
_attr_keys = st.text(st.characters(), min_size=1)
3032
_attr_values = st.recursive(
@@ -131,7 +133,7 @@ def array_metadata(
131133
draw: st.DrawFn,
132134
*,
133135
array_shapes: Callable[..., st.SearchStrategy[tuple[int, ...]]] = npst.array_shapes,
134-
zarr_formats: st.SearchStrategy[Literal[2, 3]] = zarr_formats,
136+
zarr_formats: st.SearchStrategy[ZarrFormat] = zarr_formats,
135137
attributes: SearchStrategy[Mapping[str, JSON] | None] = attrs,
136138
) -> ArrayV2Metadata | ArrayV3Metadata:
137139
zarr_format = draw(zarr_formats)
@@ -348,8 +350,8 @@ def basic_indices(
348350
shape: tuple[int, ...],
349351
min_dims: int = 0,
350352
max_dims: int | None = None,
351-
allow_newaxis: bool = False,
352-
allow_ellipsis: bool = True,
353+
allow_newaxis: TrueOrFalse = False,
354+
allow_ellipsis: TrueOrFalse = True,
353355
) -> Any:
354356
"""Basic indices without unsupported negative slices."""
355357
strategy = npst.basic_indices(
@@ -362,7 +364,7 @@ def basic_indices(
362364
lambda idxr: (
363365
not (
364366
is_negative_slice(idxr)
365-
or (isinstance(idxr, tuple) and any(is_negative_slice(idx) for idx in idxr)) # type: ignore[redundant-expr]
367+
or (isinstance(idxr, tuple) and any(is_negative_slice(idx) for idx in idxr))
366368
)
367369
)
368370
)

tests/package_with_entrypoint/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class TestDataType(Bool):
8484
_zarr_v3_name: ClassVar[Literal["test"]] = "test" # type: ignore[assignment]
8585

8686
@classmethod
87-
def from_json(cls, data: DTypeJSON, *, zarr_format: Literal[2, 3]) -> Self:
87+
def from_json(cls, data: DTypeJSON, *, zarr_format: ZarrFormat) -> Self:
8888
if zarr_format == 2 and data == {"name": cls._zarr_v3_name, "object_codec_id": None}:
8989
return cls()
9090
if zarr_format == 3 and data == cls._zarr_v3_name:

0 commit comments

Comments
 (0)