Skip to content

Commit 5b37417

Browse files
maxrjonesd-v-b
andcommitted
Match specific Errors in tests
Co-authored-by: Davis Bennett <[email protected]>
1 parent bf4589d commit 5b37417

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

src/zarr/storage/_fsspec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ async def set(
256256
self._check_writable()
257257
if not isinstance(value, Buffer):
258258
raise TypeError(
259-
f"FsspecStore.set(): `value` must a Buffer instance. Got an instance of {type(value)} instead."
259+
f"FsspecStore.set(): `value` must be a Buffer instance. Got an instance of {type(value)} instead."
260260
)
261261
path = _dereference_path(self.path, key)
262262
# write data

src/zarr/storage/_local.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ async def _set(self, key: str, value: Buffer, exclusive: bool = False) -> None:
169169
self._check_writable()
170170
assert isinstance(key, str)
171171
if not isinstance(value, Buffer):
172-
raise TypeError("LocalStore.set(): `value` must a Buffer instance")
172+
raise TypeError(
173+
f"LocalStore.set(): `value` must be a Buffer instance. Got an instance of {type(value)} instead."
174+
)
173175
path = self.root / key
174176
await asyncio.to_thread(_put, path, value, start=None, exclusive=exclusive)
175177

src/zarr/storage/_memory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ async def set(self, key: str, value: Buffer, byte_range: tuple[int, int] | None
111111
await self._ensure_open()
112112
assert isinstance(key, str)
113113
if not isinstance(value, Buffer):
114-
raise TypeError(f"Expected Buffer. Got {type(value)}.")
114+
raise TypeError(
115+
f"MemoryStore.set(): `value` must be a Buffer instance. Got an instance of {type(value)} instead."
116+
)
115117

116118
if byte_range is not None:
117119
buf = self._store_dict[key]

src/zarr/storage/_zip.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ async def set(self, key: str, value: Buffer) -> None:
211211
self._sync_open()
212212
assert isinstance(key, str)
213213
if not isinstance(value, Buffer):
214-
raise TypeError("ZipStore.set(): `value` must a Buffer instance")
214+
raise TypeError(
215+
f"ZipStore.set(): `value` must be a Buffer instance. Got an instance of {type(value)} instead."
216+
)
215217
with self._lock:
216218
self._set(key, value)
217219

src/zarr/testing/store.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async def test_store_context_manager(self, open_kwargs: dict[str, Any]) -> None:
121121
with await self.store_cls.open(**open_kwargs) as store:
122122
assert store._is_open
123123
# Test trying to open an already open store
124-
with pytest.raises(ValueError):
124+
with pytest.raises(ValueError, match="store is already open"):
125125
await store._open()
126126
assert not store._is_open
127127

@@ -131,11 +131,15 @@ async def test_read_only_store_raises(self, open_kwargs: dict[str, Any]) -> None
131131
assert store.read_only
132132

133133
# set
134-
with pytest.raises(ValueError):
134+
with pytest.raises(
135+
ValueError, match="store was opened in read-only mode and does not support writing"
136+
):
135137
await store.set("foo", self.buffer_cls.from_bytes(b"bar"))
136138

137139
# delete
138-
with pytest.raises(ValueError):
140+
with pytest.raises(
141+
ValueError, match="store was opened in read-only mode and does not support writing"
142+
):
139143
await store.delete("foo")
140144

141145
@pytest.mark.parametrize("key", ["c/0", "foo/c/0.0", "foo/0/0"])
@@ -171,7 +175,7 @@ async def test_get_raises(self, store: S) -> None:
171175
"""
172176
data_buf = self.buffer_cls.from_bytes(b"\x01\x02\x03\x04")
173177
await self.set(store, "c/0", data_buf)
174-
with pytest.raises((ValueError, TypeError)):
178+
with pytest.raises((ValueError, TypeError), match=r"Unexpected byte_range, got.*"):
175179
await store.get("c/0", prototype=default_buffer_prototype(), byte_range=(0, 2)) # type: ignore[arg-type]
176180

177181
async def test_get_many(self, store: S) -> None:
@@ -222,7 +226,7 @@ async def test_getsize_prefix(self, store: S) -> None:
222226

223227
async def test_getsize_raises(self, store: S) -> None:
224228
"""
225-
Test the result of store.getsize().
229+
Test that getsize() raise a FileNotFoundError if the key doesn't exist.
226230
"""
227231
with pytest.raises(FileNotFoundError):
228232
await store.getsize("c/1000")
@@ -266,7 +270,10 @@ async def test_set_invalid_buffer(self, store: S) -> None:
266270
"""
267271
Ensure that set raises a Type or Value Error for invalid buffer arguments.
268272
"""
269-
with pytest.raises((ValueError, TypeError)):
273+
with pytest.raises(
274+
(ValueError, TypeError),
275+
match=r"\S+\.set\(\): `value` must be a Buffer instance. Got an instance of <class 'int'> instead.",
276+
):
270277
await store.set("c/0", 0) # type: ignore[arg-type]
271278

272279
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)