Skip to content

Commit 21d6188

Browse files
committed
lint
1 parent 32cd309 commit 21d6188

File tree

5 files changed

+20
-28
lines changed

5 files changed

+20
-28
lines changed

src/zarr/core/dtype/npy/structured.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
from __future__ import annotations
22

3-
from collections.abc import Sequence
3+
from collections.abc import Mapping, Sequence
44
from dataclasses import dataclass
55
from typing import TYPE_CHECKING, ClassVar, Literal, Self, TypeGuard, cast, overload
66

77
import numpy as np
88

9-
from zarr.core.common import NamedConfig
9+
from zarr.core.common import NamedRequiredConfig, StructuredName_V2
1010
from zarr.core.dtype.common import (
1111
DataTypeValidationError,
1212
DTypeConfig_V2,
1313
DTypeJSON,
1414
HasItemSize,
15-
StructuredName_V2,
1615
v3_unstable_dtype_warning,
1716
)
1817
from zarr.core.dtype.npy.common import (
@@ -57,7 +56,7 @@ class StructuredJSON_V2(DTypeConfig_V2[StructuredName_V2, None]):
5756

5857

5958
class StructuredJSON_V3(
60-
NamedConfig[Literal["structured"], dict[str, Sequence[Sequence[str | DTypeJSON]]]]
59+
NamedRequiredConfig[Literal["structured"], Mapping[str, Sequence[Sequence[str | DTypeJSON]]]]
6160
):
6261
"""
6362
A JSON representation of a structured data type in Zarr V3.
@@ -229,13 +228,7 @@ def _check_json_v3(cls, data: DTypeJSON) -> TypeGuard[StructuredJSON_V3]:
229228
False otherwise.
230229
"""
231230

232-
return (
233-
isinstance(data, dict)
234-
and set(data.keys()) == {"name", "configuration"}
235-
and data["name"] == cls._zarr_v3_name
236-
and isinstance(data["configuration"], dict)
237-
and set(data["configuration"].keys()) == {"fields"}
238-
)
231+
return guard_type(data, StructuredJSON_V3)
239232

240233
@classmethod
241234
def _from_json_v2(cls, data: DTypeJSON) -> Self:

src/zarr/core/dtype/wrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import numpy as np
4040

4141
if TYPE_CHECKING:
42-
from zarr.core.common import JSON, ZarrFormat
43-
from zarr.core.dtype.common import DTypeJSON, DTypeSpec_V2, DTypeSpec_V3
42+
from zarr.core.common import JSON, DTypeSpec_V3, ZarrFormat
43+
from zarr.core.dtype.common import DTypeJSON, DTypeSpec_V2
4444

4545
# This the upper bound for the scalar types we support. It's numpy scalars + str,
4646
# because the new variable-length string dtype in numpy does not have a corresponding scalar type

src/zarr/core/type_check.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _resolve_type(
117117
_seen: set[Any] | None = None,
118118
) -> Any:
119119
"""
120-
Resolve type hints and ForwardRef.
120+
Resolve type hints and ForwardRef. Maintains a cache of resolved types to avoid infinite recursion.
121121
"""
122122
if _seen is None:
123123
_seen = set()
@@ -126,14 +126,12 @@ def _resolve_type(
126126
type_repr = repr(tp)
127127
if type_repr in _seen:
128128
# Return Any for recursive types to break the cycle
129-
result = Any
130-
return result
129+
return Any
131130

132131
_seen.add(type_repr)
133132

134133
try:
135-
result = _resolve_type_impl(tp, type_map, globalns, localns, _seen)
136-
return result
134+
return _resolve_type_impl(tp, type_map, globalns, localns, _seen)
137135
finally:
138136
_seen.discard(type_repr)
139137

@@ -301,8 +299,10 @@ def check_typeddict(
301299

302300
if type_map is None and len(get_args(td_type)) > 0:
303301
base_origin, base_args = _find_generic_typeddict_base(td_cls)
304-
if base_origin is not None and len(getattr(base_origin, "__parameters__", ())) != len(
305-
base_args
302+
if (
303+
base_origin is not None
304+
and base_args is not None
305+
and len(getattr(base_origin, "__parameters__", ())) != len(base_args)
306306
):
307307
return TypeCheckResult(False, [f"{path} type parameter count mismatch in generic base"])
308308

@@ -462,7 +462,7 @@ def _get_typeddict_metadata(
462462
base_origin, base_args = _find_generic_typeddict_base(td_cls)
463463
if base_origin is not None:
464464
tvars = getattr(base_origin, "__parameters__", ())
465-
type_map = dict(zip(tvars, base_args, strict=False))
465+
type_map = dict(zip(tvars, base_args, strict=False)) # type: ignore[arg-type]
466466

467467
mod = sys.modules.get(base_origin.__module__)
468468
globalns = vars(mod) if mod else {}

src/zarr/registry.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from importlib.metadata import entry_points as get_entry_points
66
from typing import TYPE_CHECKING, Any, Generic, TypeVar
77

8-
from zarr.core.common import CodecJSON_V2
98
from zarr.core.config import BadConfigError, config
109
from zarr.core.dtype import data_type_registry
1110
from zarr.errors import ZarrUserWarning
@@ -22,7 +21,7 @@
2221
)
2322
from zarr.abc.numcodec import Numcodec
2423
from zarr.core.buffer import Buffer, NDBuffer
25-
from zarr.core.common import JSON
24+
from zarr.core.common import JSON, CodecJSON_V2
2625

2726
__all__ = [
2827
"Registry",

tests/test_type_check.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from typing_extensions import ReadOnly, TypedDict
88

99
from src.zarr.core.type_check import check_type
10-
from zarr.core.common import ArrayMetadataJSON_V3, NamedConfig
11-
from zarr.core.dtype.common import DTypeConfig_V2, DTypeSpec_V2, DTypeSpec_V3, StructuredName_V2
10+
from zarr.core.common import ArrayMetadataJSON_V3, DTypeSpec_V3, NamedConfig, StructuredName_V2
11+
from zarr.core.dtype.common import DTypeConfig_V2, DTypeSpec_V2
1212
from zarr.core.dtype.npy.structured import StructuredJSON_V2
1313
from zarr.core.dtype.npy.time import TimeConfig
1414

@@ -269,7 +269,7 @@ def test_zarr_v2_metadata(optionals: dict[str, object]) -> None:
269269
"codecs": ("bytes",),
270270
"attributes": {"a": 1, "b": 2},
271271
"data_type": "uint8",
272-
} | optionals
272+
} | optionals # type: ignore[assignment]
273273
result = check_type(meta, ArrayMetadataJSON_V3)
274274
assert result.success
275275

@@ -287,15 +287,15 @@ def test_typeddict_extra_keys_allowed() -> None:
287287
class X(TypedDict):
288288
a: int
289289

290-
b: X = {"a": 1, "b": 2}
290+
b: X = {"a": 1, "b": 2} # type: ignore[typeddict-unknown-key]
291291
result = check_type(b, X)
292292
assert result.success
293293

294294

295295
def test_typeddict_readonly_notrequired() -> None:
296296
class X(TypedDict):
297297
a: ReadOnly[NotRequired[int]]
298-
b: NotRequired[ReadOnly[int]] # type: ignore[typeddict-unknown-key]
298+
b: NotRequired[ReadOnly[int]]
299299
c: Annotated[ReadOnly[NotRequired[int]], 10]
300300
d: int
301301

0 commit comments

Comments
 (0)