55import numpy as np
66import pytest
77
8+ import zarr
89from tests .test_codecs .conftest import Expect , ExpectErr
9- from zarr .codecs .scale_offset import ScaleOffset
10+ from zarr .codecs .scale_offset import ScaleOffset , _decode , _encode
11+ from zarr .core .buffer .core import default_buffer_prototype
12+ from zarr .storage ._memory import MemoryStore
1013
1114# ---------------------------------------------------------------------------
1215# Serialization
@@ -279,7 +282,6 @@ def test_rejects_zero_scale() -> None:
279282)
280283def test_rejects_unrepresentable_scale_offset (case : ExpectErr [dict [str , Any ]]) -> None :
281284 """Scale/offset values that can't be represented in the array dtype are rejected."""
282- import zarr
283285
284286 with pytest .raises (case .exception_cls , match = case .msg ):
285287 zarr .create_array (
@@ -295,7 +297,6 @@ def test_rejects_unrepresentable_scale_offset(case: ExpectErr[dict[str, Any]]) -
295297
296298def test_dtype_preservation () -> None :
297299 """Integer scale/offset arithmetic preserves the array dtype when division is exact."""
298- import zarr
299300
300301 arr = zarr .create_array (
301302 store = {},
@@ -312,11 +313,8 @@ def test_dtype_preservation() -> None:
312313 np .testing .assert_array_equal (arr [:], data )
313314
314315
315- def test_integer_decode_rejects_non_exact_division () -> None :
316+ async def test_integer_decode_rejects_non_exact_division () -> None :
316317 """Decoding an integer array raises when the stored value isn't divisible by scale."""
317- import zarr
318- from zarr .storage import MemoryStore
319-
320318 store = MemoryStore ()
321319 arr = zarr .create_array (
322320 store = store ,
@@ -329,20 +327,15 @@ def test_integer_decode_rejects_non_exact_division() -> None:
329327 )
330328 # Write raw encoded bytes directly so we can inject a value that isn't divisible by scale.
331329 # Array layout: int8 [2, 3, 4]; 3 % 2 != 0, so decode must fail.
332- import asyncio
333-
334- from zarr .core .buffer import default_buffer_prototype
335330
336331 buf = default_buffer_prototype ().buffer .from_bytes (np .array ([2 , 3 , 4 ], dtype = "int8" ).tobytes ())
337- asyncio . run ( arr .store_path .store .set ("c/0" , buf ) )
332+ await arr .store_path .store .set ("c/0" , buf )
338333 with pytest .raises (ValueError , match = "non-zero remainder" ):
339334 arr [:]
340335
341336
342337def test_encode_rejects_signed_integer_overflow () -> None :
343338 """Encoding raises when (value - offset) * scale exceeds the target integer range."""
344- import zarr
345-
346339 arr = zarr .create_array (
347340 store = {},
348341 shape = (3 ,),
@@ -359,8 +352,6 @@ def test_encode_rejects_signed_integer_overflow() -> None:
359352
360353def test_encode_rejects_unsigned_integer_underflow () -> None :
361354 """Encoding raises when value - offset underflows an unsigned dtype."""
362- import zarr
363-
364355 arr = zarr .create_array (
365356 store = {},
366357 shape = (3 ,),
@@ -377,8 +368,6 @@ def test_encode_rejects_unsigned_integer_underflow() -> None:
377368
378369def test_float32_dtype_preserved () -> None :
379370 """float32 arrays survive encode+decode without being promoted to float64."""
380- from zarr .codecs .scale_offset import _decode , _encode
381-
382371 arr = np .arange (100 , dtype = "float32" )
383372 offset = np .float32 (5.0 )
384373 scale = np .float32 (0.25 )
@@ -390,8 +379,6 @@ def test_float32_dtype_preserved() -> None:
390379
391380def test_float_encode_rejects_wider_scalar () -> None :
392381 """A float64 scalar passed with a float32 array must not silently widen the result."""
393- from zarr .codecs .scale_offset import _encode
394-
395382 arr = np .arange (10 , dtype = "float32" )
396383 # A numpy float64 scalar (not a Python float — NEP 50 exempts those) mixed with a
397384 # float32 ndarray promotes to float64. The codec must reject that.
@@ -401,21 +388,13 @@ def test_float_encode_rejects_wider_scalar() -> None:
401388
402389def test_float_decode_rejects_wider_scalar () -> None :
403390 """A float64 scalar passed with a float32 array must not silently widen on decode."""
404- from zarr .codecs .scale_offset import _decode
405-
406391 arr = np .arange (10 , dtype = "float32" )
407392 with pytest .raises (ValueError , match = "changed dtype from float32 to float64" ):
408393 _decode (arr , np .float64 (5.0 ), np .float64 (0.25 ), scale_repr = 0.25 )
409394
410395
411- def test_decode_rejects_integer_overflow_on_offset_add () -> None :
396+ async def test_decode_rejects_integer_overflow_on_offset_add () -> None :
412397 """Decoding raises when quotient + offset overflows the target integer dtype."""
413- import asyncio
414-
415- import zarr
416- from zarr .core .buffer import default_buffer_prototype
417- from zarr .storage import MemoryStore
418-
419398 store = MemoryStore ()
420399 arr = zarr .create_array (
421400 store = store ,
@@ -430,6 +409,6 @@ def test_decode_rejects_integer_overflow_on_offset_add() -> None:
430409 buf = default_buffer_prototype ().buffer .from_bytes (
431410 np .array ([0 , 50 , 100 ], dtype = "int8" ).tobytes ()
432411 )
433- asyncio . run ( arr .store_path .store .set ("c/0" , buf ) )
412+ await arr .store_path .store .set ("c/0" , buf )
434413 with pytest .raises (ValueError , match = "outside the range of dtype int8" ):
435414 arr [:]
0 commit comments