Skip to content

Commit 2598cc7

Browse files
committed
(chore) type hints for tests
1 parent 8d15c02 commit 2598cc7

35 files changed

+261
-256
lines changed

numcodecs/tests/common.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
from __future__ import annotations
12
import array
23
import json as _json
34
import os
45
from glob import glob
6+
from typing import TYPE_CHECKING
7+
if TYPE_CHECKING:
8+
from typing import Sequence
59

610
import numpy as np
11+
from numcodecs.abc import Codec
712
import pytest
813
from numpy.testing import assert_array_almost_equal, assert_array_equal
914

@@ -27,7 +32,7 @@
2732
]
2833

2934

30-
def compare_arrays(arr, res, precision=None):
35+
def compare_arrays(arr: np.ndarray, res: np.ndarray, precision: int | None = None) -> None:
3136
# ensure numpy array with matching dtype
3237
res = ensure_ndarray(res).view(arr.dtype)
3338

@@ -47,7 +52,7 @@ def compare_arrays(arr, res, precision=None):
4752
assert_array_almost_equal(arr, res, decimal=precision)
4853

4954

50-
def check_encode_decode(arr, codec, precision=None):
55+
def check_encode_decode(arr: np.ndarray, codec: Codec, precision: int | None = None) -> None:
5156
# N.B., watch out here with blosc compressor, if the itemsize of
5257
# the source buffer is different then the results of encoding
5358
# (i.e., compression) may be different. Hence we *do not* require that
@@ -115,7 +120,7 @@ def check_encode_decode(arr, codec, precision=None):
115120
compare_arrays(arr, out, precision=precision)
116121

117122

118-
def check_encode_decode_partial(arr, codec, precision=None):
123+
def check_encode_decode_partial(arr: np.ndarray, codec: Codec, precision: int | None = None) -> None:
119124
# N.B., watch out here with blosc compressor, if the itemsize of
120125
# the source buffer is different then the results of encoding
121126
# (i.e., compression) may be different. Hence we *do not* require that
@@ -183,7 +188,7 @@ def check_encode_decode_partial(arr, codec, precision=None):
183188
compare_arrays(compare_arr, out, precision=precision)
184189

185190

186-
def assert_array_items_equal(res, arr):
191+
def assert_array_items_equal(res: np.ndarray, arr: np.ndarray) -> None:
187192
assert isinstance(res, np.ndarray)
188193
res = res.reshape(-1, order='A')
189194
arr = arr.reshape(-1, order='A')
@@ -204,7 +209,7 @@ def assert_array_items_equal(res, arr):
204209
assert a == r
205210

206211

207-
def check_encode_decode_array(arr, codec):
212+
def check_encode_decode_array(arr: np.ndarray, codec: Codec) -> None:
208213
enc = codec.encode(arr)
209214
dec = codec.decode(enc)
210215
assert_array_items_equal(arr, dec)
@@ -218,7 +223,7 @@ def check_encode_decode_array(arr, codec):
218223
assert_array_items_equal(arr, dec)
219224

220225

221-
def check_encode_decode_array_to_bytes(arr, codec):
226+
def check_encode_decode_array_to_bytes(arr: np.ndarray, codec: Codec) -> None:
222227
enc = codec.encode(arr)
223228
dec = codec.decode(enc)
224229
assert_array_items_equal(arr, dec)
@@ -228,21 +233,21 @@ def check_encode_decode_array_to_bytes(arr, codec):
228233
assert_array_items_equal(arr, out)
229234

230235

231-
def check_config(codec):
236+
def check_config(codec: Codec) -> None:
232237
config = codec.get_config()
233238
# round-trip through JSON to check serialization
234239
config = _json.loads(_json.dumps(config))
235240
assert codec == get_codec(config)
236241

237242

238-
def check_repr(stmt):
243+
def check_repr(stmt: str) -> None:
239244
# check repr matches instantiation statement
240245
codec = eval(stmt)
241246
actual = repr(codec)
242247
assert stmt == actual
243248

244249

245-
def check_backwards_compatibility(codec_id, arrays, codecs, precision=None, prefix=None):
250+
def check_backwards_compatibility(codec_id: str, arrays: Sequence[np.ndarray], codecs: Sequence[Codec], precision: Sequence[int | None] | None = None, prefix: str | None = None) -> None:
246251
# setup directory to hold data fixture
247252
if prefix:
248253
fixture_dir = os.path.join('fixture', codec_id, prefix)
@@ -312,7 +317,7 @@ def check_backwards_compatibility(codec_id, arrays, codecs, precision=None, pref
312317
assert arr_bytes == ensure_bytes(dec)
313318

314319

315-
def check_err_decode_object_buffer(compressor):
320+
def check_err_decode_object_buffer(compressor: Codec) -> None:
316321
# cannot decode directly into object array, leads to segfaults
317322
a = np.arange(10)
318323
enc = compressor.encode(a)
@@ -321,14 +326,14 @@ def check_err_decode_object_buffer(compressor):
321326
compressor.decode(enc, out=out)
322327

323328

324-
def check_err_encode_object_buffer(compressor):
329+
def check_err_encode_object_buffer(compressor: Codec) -> None:
325330
# compressors cannot encode object array
326331
a = np.array(['foo', 'bar', 'baz'], dtype=object)
327332
with pytest.raises(TypeError):
328333
compressor.encode(a)
329334

330335

331-
def check_max_buffer_size(codec):
336+
def check_max_buffer_size(codec: Codec) -> None:
332337
for max_buffer_size in (4, 64, 1024):
333338
old_max_buffer_size = codec.max_buffer_size
334339
try:

numcodecs/tests/test_astype.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
]
2121

2222

23-
def test_encode_decode():
23+
def test_encode_decode() -> None:
2424
for arr in arrays:
2525
codec = AsType(encode_dtype=arr.dtype, decode_dtype=arr.dtype)
2626
check_encode_decode(arr, codec)
2727

2828

29-
def test_decode():
29+
def test_decode() -> None:
3030
encode_dtype, decode_dtype = '<i4', '<i8'
3131
codec = AsType(encode_dtype=encode_dtype, decode_dtype=decode_dtype)
3232
arr = np.arange(10, 20, 1, dtype=encode_dtype)
@@ -36,7 +36,7 @@ def test_decode():
3636
assert np.dtype(decode_dtype) == actual.dtype
3737

3838

39-
def test_encode():
39+
def test_encode() -> None:
4040
encode_dtype, decode_dtype = '<i4', '<i8'
4141
codec = AsType(encode_dtype=encode_dtype, decode_dtype=decode_dtype)
4242
arr = np.arange(10, 20, 1, dtype=decode_dtype)
@@ -46,17 +46,17 @@ def test_encode():
4646
assert np.dtype(encode_dtype) == actual.dtype
4747

4848

49-
def test_config():
49+
def test_config() -> None:
5050
encode_dtype, decode_dtype = '<i4', '<i8'
5151
codec = AsType(encode_dtype=encode_dtype, decode_dtype=decode_dtype)
5252
check_config(codec)
5353

5454

55-
def test_repr():
55+
def test_repr() -> None:
5656
check_repr("AsType(encode_dtype='<i4', decode_dtype='<i2')")
5757

5858

59-
def test_backwards_compatibility():
59+
def test_backwards_compatibility() -> None:
6060
# integers
6161
arrs = [
6262
np.arange(1000, dtype='<i4'),

numcodecs/tests/test_base64.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,43 @@
3737
]
3838

3939

40-
def test_encode_decode():
40+
def test_encode_decode() -> None:
4141
for arr, codec in itertools.product(arrays, codecs):
4242
check_encode_decode(arr, codec)
4343

4444

45-
def test_repr():
45+
def test_repr() -> None:
4646
check_repr("Base64()")
4747

4848

49-
def test_eq():
49+
def test_eq() -> None:
5050
assert Base64() == Base64()
5151
assert not Base64() != Base64()
5252
assert Base64() != "foo"
5353
assert "foo" != Base64()
5454
assert not Base64() == "foo"
5555

5656

57-
def test_backwards_compatibility():
57+
def test_backwards_compatibility() -> None:
5858
check_backwards_compatibility(Base64.codec_id, arrays, codecs)
5959

6060

61-
def test_err_decode_object_buffer():
61+
def test_err_decode_object_buffer() -> None:
6262
check_err_decode_object_buffer(Base64())
6363

6464

65-
def test_err_encode_object_buffer():
65+
def test_err_encode_object_buffer() -> None:
6666
check_err_encode_object_buffer(Base64())
6767

6868

69-
def test_err_encode_list():
69+
def test_err_encode_list() -> None:
7070
data = ["foo", "bar", "baz"]
7171
for codec in codecs:
7272
with pytest.raises(TypeError):
7373
codec.encode(data)
7474

7575

76-
def test_err_encode_non_contiguous():
76+
def test_err_encode_non_contiguous() -> None:
7777
# non-contiguous memory
7878
arr = np.arange(1000, dtype="i4")[::2]
7979
for codec in codecs:

numcodecs/tests/test_bitround.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88

99
# TODO: add other dtypes
1010
@pytest.fixture(params=["float32", "float64"])
11-
def dtype(request):
11+
def dtype(request: pytest.FixtureRequest) -> str:
1212
return request.param
1313

1414

15-
def round(data, keepbits):
15+
def round(data: np.ndarray, keepbits: int) -> np.ndarray:
1616
codec = BitRound(keepbits=keepbits)
1717
data = data.copy() # otherwise overwrites the input
1818
encoded = codec.encode(data)
1919
return codec.decode(encoded)
2020

2121

22-
def test_round_zero_to_zero(dtype):
22+
def test_round_zero_to_zero(dtype: str) -> None:
2323
a = np.zeros((3, 2), dtype=dtype)
2424
# Don't understand Milan's original test:
2525
# How is it possible to have negative keepbits?
@@ -29,21 +29,21 @@ def test_round_zero_to_zero(dtype):
2929
np.testing.assert_equal(a, ar)
3030

3131

32-
def test_round_one_to_one(dtype):
32+
def test_round_one_to_one(dtype: str) -> None:
3333
a = np.ones((3, 2), dtype=dtype)
3434
for k in range(max_bits[dtype]):
3535
ar = round(a, k)
3636
np.testing.assert_equal(a, ar)
3737

3838

39-
def test_round_minus_one_to_minus_one(dtype):
39+
def test_round_minus_one_to_minus_one(dtype: str) -> None:
4040
a = -np.ones((3, 2), dtype=dtype)
4141
for k in range(max_bits[dtype]):
4242
ar = round(a, k)
4343
np.testing.assert_equal(a, ar)
4444

4545

46-
def test_no_rounding(dtype):
46+
def test_no_rounding(dtype: str) -> None:
4747
a = np.random.random_sample((300, 200)).astype(dtype)
4848
keepbits = max_bits[dtype]
4949
ar = round(a, keepbits)
@@ -53,7 +53,7 @@ def test_no_rounding(dtype):
5353
APPROX_KEEPBITS = {"float32": 11, "float64": 18}
5454

5555

56-
def test_approx_equal(dtype):
56+
def test_approx_equal(dtype: str) -> None:
5757
a = np.random.random_sample((300, 200)).astype(dtype)
5858
ar = round(a, APPROX_KEEPBITS[dtype])
5959
# Mimic julia behavior - https://docs.julialang.org/en/v1/base/math/#Base.isapprox
@@ -64,15 +64,15 @@ def test_approx_equal(dtype):
6464
np.testing.assert_allclose(a, ar, rtol=rtol)
6565

6666

67-
def test_idempotence(dtype):
67+
def test_idempotence(dtype: str) -> None:
6868
a = np.random.random_sample((300, 200)).astype(dtype)
6969
for k in range(20):
7070
ar = round(a, k)
7171
ar2 = round(a, k)
7272
np.testing.assert_equal(ar, ar2)
7373

7474

75-
def test_errors():
75+
def test_errors() -> None:
7676
with pytest.raises(ValueError):
7777
BitRound(keepbits=99).encode(np.array([0], dtype="float32"))
7878
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)