11import enum
22import json
33import warnings
4- from typing import cast
4+ from typing import Any , cast
55
66import numcodecs
77import numpy as np
1111import zarr
1212from zarr .abc .codec import SupportsSyncCodec
1313from zarr .codecs import BloscCodec
14- from zarr .codecs .blosc import BloscCname , BloscCnameLiteral , BloscShuffle , BloscShuffleLiteral
14+ from zarr .codecs .blosc import BloscCname , BloscShuffle , BloscShuffleLiteral
1515from zarr .core .array_spec import ArrayConfig , ArraySpec
1616from zarr .core .buffer import default_buffer_prototype
1717from zarr .core .dtype import UInt16 , get_data_type_from_native_dtype
@@ -150,27 +150,43 @@ def test_blosc_codec_sync_roundtrip() -> None:
150150 np .testing .assert_array_equal (arr , result )
151151
152152
153- def test_blosc_shuffle_member_access_warns () -> None :
154- with pytest .warns (DeprecationWarning , match = "BloscShuffle.shuffle" ):
155- value = BloscShuffle .shuffle
156- assert value == "shuffle"
157-
158-
159- def test_blosc_cname_member_access_warns () -> None :
160- with pytest .warns (DeprecationWarning , match = "BloscCname.zstd" ):
161- value = BloscCname .zstd
162- assert value == "zstd"
153+ @pytest .mark .parametrize (
154+ ("enum_cls" , "member" , "expected" ),
155+ [
156+ (BloscShuffle , "shuffle" , "shuffle" ),
157+ (BloscCname , "zstd" , "zstd" ),
158+ ],
159+ )
160+ def test_blosc_enum_member_access_warns (enum_cls : type , member : str , expected : str ) -> None :
161+ """
162+ Accessing a member on the deprecated BloscShuffle / BloscCname classes
163+ emits a DeprecationWarning and resolves to the equivalent literal string.
164+ """
165+ match = f"{ enum_cls .__name__ } .{ member } "
166+ with pytest .warns (DeprecationWarning , match = match ):
167+ value = getattr (enum_cls , member )
168+ assert value == expected
163169
164170
165171def test_blosc_enum_classes_import_silently () -> None :
172+ """
173+ Importing the deprecated enum classes by name must not emit a warning;
174+ only member access does. This guards against the blosc module accidentally
175+ triggering its own deprecation warnings when it (or zarr) is imported.
176+ """
166177 with warnings .catch_warnings ():
167178 warnings .simplefilter ("error" )
168179 from zarr .codecs .blosc import BloscCname as _BloscCname # noqa: F401
169180 from zarr .codecs .blosc import BloscShuffle as _BloscShuffle # noqa: F401
170181
171182
172183def test_blosc_codec_init_with_enum_instance_warns () -> None :
173- """A real enum.Enum instance must trigger the init-level deprecation warning."""
184+ """
185+ Passing a real `enum.Enum` instance to BloscCodec.__init__ (e.g. an
186+ instance materialized before the deprecation shim was introduced) must
187+ trigger the init-level deprecation warning and still normalize the value
188+ to the corresponding literal string.
189+ """
174190
175191 class LegacyShuffle (enum .Enum ):
176192 bitshuffle = "bitshuffle"
@@ -187,18 +203,25 @@ class LegacyCname(enum.Enum):
187203 assert codec .shuffle == "bitshuffle"
188204
189205
190- def test_blosc_codec_rejects_unknown_cname () -> None :
191- with pytest .raises (ValueError , match = "cname must be one of" ):
192- BloscCodec (cname = cast (BloscCnameLiteral , "not-a-codec" ))
193-
194-
195- def test_blosc_codec_rejects_unknown_shuffle () -> None :
196- with pytest .raises (ValueError , match = "shuffle must be one of" ):
197- BloscCodec (shuffle = cast (BloscShuffleLiteral , "not-a-shuffle" ))
206+ @pytest .mark .parametrize ("param" , ["cname" , "shuffle" ])
207+ def test_blosc_codec_rejects_unknown (param : str ) -> None :
208+ """
209+ BloscCodec.__init__ raises ValueError when given a string outside the
210+ allowed set for `cname` or `shuffle`, and the error message names the
211+ offending parameter.
212+ """
213+ kwargs : dict [str , Any ] = {param : f"not-a-{ param } " }
214+ with pytest .raises (ValueError , match = f"{ param } must be one of" ):
215+ BloscCodec (** kwargs )
198216
199217
200- def test_blosc_enum_attribute_error_for_unknown_member () -> None :
201- with pytest .raises (AttributeError ):
202- _ = BloscShuffle .not_a_member
218+ @pytest .mark .parametrize ("enum_cls" , [BloscShuffle , BloscCname ])
219+ def test_blosc_enum_attribute_error_for_unknown_member (enum_cls : type ) -> None :
220+ """
221+ Attribute access for a name that is not a known member of the deprecated
222+ enum classes falls through to AttributeError, matching the behavior of a
223+ regular class.
224+ """
225+ unknown_name = "not_a_member"
203226 with pytest .raises (AttributeError ):
204- _ = BloscCname . not_a_member
227+ getattr ( enum_cls , unknown_name )
0 commit comments