Skip to content

Commit f9284d8

Browse files
authored
Merge pull request #376 from t20100/update-_init
Removed usage of `FilterBase` in public API typing; Replaced `FilterBase._init` with standard `__init__`
2 parents 2bffcfe + 3e0bcf8 commit f9284d8

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

src/hdf5plugin/_filters.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class FilterBase(h5py.filters.FilterRefBase):
7575
filter_id: int
7676
filter_name: str
7777

78-
def _init(
78+
def __init__(
7979
self,
8080
filter_options: tuple[int, ...] = (),
8181
config: Mapping[str, int | float | bool | str] | None = None,
@@ -189,7 +189,7 @@ def __init__(
189189
if cname == "zstd":
190190
filter_options += (clevel,)
191191
config["clevel"] = clevel
192-
self._init(filter_options, config)
192+
super().__init__(filter_options, config)
193193

194194
@property
195195
def nelems(self) -> int:
@@ -294,7 +294,7 @@ def __init__(
294294
if shuffle not in (self.NOSHUFFLE, self.SHUFFLE, self.BITSHUFFLE):
295295
raise ValueError(f"shuffle={shuffle} is not supported")
296296

297-
self._init(
297+
super().__init__(
298298
filter_options=(0, 0, 0, 0, clevel, shuffle, compression),
299299
config={"cname": cname, "clevel": clevel, "shuffle": shuffle},
300300
)
@@ -411,7 +411,7 @@ def __init__(
411411
self.TRUNC_PREC,
412412
):
413413
raise ValueError(f"filters={filters} is not supported")
414-
self._init(
414+
super().__init__(
415415
filter_options=(0, 0, 0, 0, clevel, filters, compression),
416416
config={"cname": cname, "clevel": clevel, "filters": filters},
417417
)
@@ -480,7 +480,7 @@ def __init__(self, blocksize: int = 9):
480480
if not 1 <= blocksize <= 9:
481481
raise ValueError("blocksize must be in the range [1, 9]")
482482

483-
self._init(
483+
super().__init__(
484484
filter_options=(blocksize,),
485485
config={"blocksize": blocksize},
486486
)
@@ -525,7 +525,7 @@ def __init__(self) -> None:
525525
"The FciDecomp filter is not available as hdf5plugin was not built with C++11.\n"
526526
"You may need to reinstall hdf5plugin with a recent version of pip, or rebuild it with a newer compiler."
527527
)
528-
self._init(filter_options=(), config={})
528+
super().__init__(filter_options=(), config={})
529529

530530
@classmethod
531531
def _from_filter_options(cls, filter_options: tuple[int, ...]) -> FciDecomp:
@@ -559,7 +559,7 @@ def __init__(self, nbytes: int = 0):
559559
nbytes = int(nbytes)
560560
if not 0 <= nbytes <= 0x7E000000:
561561
raise ValueError("clevel must be in the range [0, 2113929216]")
562-
self._init(
562+
super().__init__(
563563
filter_options=(nbytes,),
564564
config={"nbytes": nbytes},
565565
)
@@ -729,7 +729,7 @@ def __init__(
729729
filter_options = ()
730730
config = {}
731731

732-
self._init(filter_options, config)
732+
super().__init__(filter_options, config)
733733

734734
# From zfp.h
735735
_ZFP_MIN_BITS = 1 # minimum number of bits per block
@@ -915,7 +915,7 @@ def __init__(
915915
mode = 1
916916
quality = 16 if rate is None else rate
917917

918-
self._init(
918+
super().__init__(
919919
filter_options=self.__pack_options(mode, quality, swap, missing_value_mode),
920920
config={
921921
mode_name: quality,
@@ -1126,7 +1126,7 @@ def __init__(
11261126
logger.info(f"SZ mode {sz_mode} used.")
11271127
logger.info(f"filter options {filter_options}")
11281128

1129-
self._init(filter_options, config)
1129+
super().__init__(filter_options, config)
11301130

11311131
@classmethod
11321132
def _from_filter_options(cls, filter_options: tuple[int, ...]) -> SZ:
@@ -1227,7 +1227,7 @@ def __init__(
12271227
if len(filter_options) != 9:
12281228
raise IndexError("Invalid number of arguments")
12291229

1230-
self._init(filter_options, config)
1230+
super().__init__(filter_options, config)
12311231

12321232
@classmethod
12331233
def _from_filter_options(cls, filter_options: tuple[int, ...]) -> SZ3:
@@ -1259,7 +1259,7 @@ def _from_filter_options(cls, filter_options: tuple[int, ...]) -> SZ3:
12591259

12601260

12611261
class Zstd(FilterBase):
1262-
"""``h5py.Group.create_dataset``'s compression arguments for using FciDecomp filter.
1262+
"""``h5py.Group.create_dataset``'s compression arguments for using Zstd filter.
12631263
12641264
.. code-block:: python
12651265
@@ -1280,7 +1280,7 @@ class Zstd(FilterBase):
12801280
def __init__(self, clevel: int = 3):
12811281
if not 1 <= clevel <= 22:
12821282
raise ValueError("clevel must be in the range [1, 22]")
1283-
self._init(
1283+
super().__init__(
12841284
filter_options=(clevel,),
12851285
config={"clevel": clevel},
12861286
)

src/hdf5plugin/_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def get_config() -> HDF5PluginConfig:
197197

198198
def get_filters(
199199
filters: int | str | tuple[int | str, ...] = tuple(FILTERS.keys()),
200-
) -> tuple[type[FilterBase], ...]:
200+
) -> tuple[type[h5py.filters.FilterRefBase], ...]:
201201
"""Returns selected filter classes.
202202
203203
By default it returns all filter classes.
@@ -232,7 +232,7 @@ def get_filters(
232232

233233
def from_filter_options(
234234
filter_id: int | str, filter_options: tuple[int, ...]
235-
) -> FilterBase:
235+
) -> h5py.filters.FilterRefBase:
236236
"""Returns corresponding compression filter configuration instance.
237237
238238
.. code-block:: python
@@ -281,7 +281,7 @@ def register(
281281

282282
status = True
283283
for filter_class in filter_classes:
284-
filter_name = filter_class.filter_name
284+
filter_name = cast(FilterBase, filter_class).filter_name
285285
if not force and is_filter_available(filter_name) is True:
286286
logger.info(f"{filter_name} filter already loaded, skip it.")
287287
continue

src/hdf5plugin/test.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import shutil
3232
import tempfile
3333
import unittest
34-
from typing import Any
34+
from typing import Any, cast
3535

3636
import h5py
3737
import numpy
@@ -702,7 +702,9 @@ class TestFromFilterOptionsRoundtrip(unittest.TestCase):
702702
"""Test from_filter_options function roundtrip"""
703703

704704
def _test(
705-
self, compression_filter: _filters.FilterBase, data: numpy.ndarray[Any, Any]
705+
self,
706+
compression_filter: h5py.filters.FilterRefBase,
707+
data: numpy.ndarray[Any, Any],
706708
):
707709
with h5py.File("in_memory", "w", driver="core", backing_store=False) as h5f:
708710
h5f.create_dataset(
@@ -802,7 +804,8 @@ def testGetConfigRoundtrip(self):
802804
filter_instance = filter_class()
803805
config = filter_instance.get_config()
804806
self.assertIsInstance(config, dict)
805-
self.assertEqual(filter_instance, filter_class(**config))
807+
cls = cast(type[h5py.filters.FilterRefBase], filter_class)
808+
self.assertEqual(filter_instance, cls(**config))
806809

807810

808811
class TestFilterProperties(unittest.TestCase):
@@ -921,6 +924,7 @@ def test_register_single_filter_by_id(self):
921924
for filter_name in BUILD_CONFIG.embedded_filters:
922925
with self.subTest(name=filter_name):
923926
filter_class = hdf5plugin.get_filters(filter_name)[0]
927+
assert filter_class.filter_id is not None
924928
status = hdf5plugin.register(filter_class.filter_id, force=True)
925929
self.assertTrue(status)
926930
self._simple_test(filter_name)
@@ -955,7 +959,7 @@ def testSelection(self):
955959
"""Get selected filters"""
956960
tests: dict[
957961
int | str | tuple[int | str, ...],
958-
tuple[type[_filters.FilterBase], ...],
962+
tuple[type[h5py.filters.FilterRefBase], ...],
959963
] = {
960964
"blosc": (hdf5plugin.Blosc,),
961965
("blosc", "zfp"): (hdf5plugin.Blosc, hdf5plugin.Zfp),

0 commit comments

Comments
 (0)