Skip to content

Commit 8b882b7

Browse files
committed
fix shapelike parser and tests; clean up imports
1 parent 7a10b85 commit 8b882b7

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/zarr/v3/common.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
from __future__ import annotations
2-
2+
from typing import TYPE_CHECKING, Union, Tuple, Iterable, Dict, List, TypeVar, Protocol
33
import asyncio
44
from asyncio import AbstractEventLoop
55
import contextvars
66
from dataclasses import dataclass
77
from enum import Enum
88
import functools
9-
from typing import (
10-
Any,
11-
Awaitable,
12-
Callable,
13-
Dict,
14-
Iterable,
15-
Iterator,
16-
List,
17-
Literal,
18-
Optional,
19-
Protocol,
20-
Tuple,
21-
Type,
22-
TypeVar,
23-
Union,
24-
)
9+
10+
if TYPE_CHECKING:
11+
from typing import (
12+
Any,
13+
Awaitable,
14+
Callable,
15+
Iterator,
16+
Literal,
17+
Optional,
18+
Type,
19+
)
2520

2621
import numpy as np
2722

@@ -162,10 +157,15 @@ def parse_name(data: JSON, expected: str) -> str:
162157
def parse_shapelike(data: Any) -> Tuple[int, ...]:
163158
if not isinstance(data, Iterable):
164159
raise TypeError(f"Expected an iterable. Got {data} instead.")
165-
if not all(lambda v: (isinstance(a, int) and a > 0) for a in data):
166-
msg = f"Expected an iterable of integers each greater than 0. Got {data} instead."
160+
data_tuple = tuple(data)
161+
if len(data_tuple) == 0:
162+
raise ValueError("Expected at least one element. Got 0.")
163+
if not all(isinstance(v, int) for v in data_tuple):
164+
msg = f"Expected an iterable of integers. Got {type(data)} instead."
167165
raise TypeError(msg)
168-
return tuple(data)
166+
if not all(lambda v: v > 0 for v in data_tuple):
167+
raise ValueError(f"All values must be greater than 0. Got {data}.")
168+
return data_tuple
169169

170170

171171
def parse_dtype(data: Any) -> np.dtype:

tests/v3/test_common.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import annotations
2-
from typing import TYPE_CHECKING
2+
from typing import TYPE_CHECKING, Iterable
33

44
if TYPE_CHECKING:
5-
from typing import Literal, Any, Iterable, Tuple
5+
from typing import Literal, Any, Tuple
66

77
import numpy as np
88
from src.zarr.v3.common import parse_indexing_order, parse_shapelike
@@ -65,13 +65,17 @@ def parse_indexing_order_valid(data: Literal["C", "F"]):
6565
assert parse_indexing_order(data) == data
6666

6767

68-
@pytest.mark.parametrize("data", [10, ("0", 1, 2, 3), {0: "0"}, []])
68+
@pytest.mark.parametrize("data", [10, ("0", 1, 2, 3), {"0": "0"}, []])
6969
def test_parse_shapelike_invalid(data: Any):
70-
if not isinstance(data, Iterable):
71-
with pytest.raises(TypeError, match="Expected an iterable."):
72-
parse_shapelike(data)
70+
if isinstance(data, Iterable):
71+
if len(data) == 0:
72+
with pytest.raises(ValueError, match="Expected at least one element."):
73+
parse_shapelike(data)
74+
else:
75+
with pytest.raises(TypeError, match="Expected an iterable of integers"):
76+
parse_shapelike(data)
7377
else:
74-
with pytest.raises(TypeError, match="Expected an iterable of integers"):
78+
with pytest.raises(TypeError, match="Expected an iterable."):
7579
parse_shapelike(data)
7680

7781

0 commit comments

Comments
 (0)