File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ import warnings
1
2
from collections .abc import MutableMapping
2
3
3
4
from zarr ._storage .store import Store , StoreV3
@@ -128,6 +129,27 @@ def put(self, d):
128
129
self ._write_op (self ._put_nosync , dict (attributes = d ))
129
130
130
131
def _put_nosync (self , d ):
132
+
133
+ d_to_check = d if self ._version == 2 else d ["attributes" ]
134
+ if not all (isinstance (item , str ) for item in d_to_check ):
135
+ # TODO: Raise an error for non-string keys
136
+ # raise TypeError("attribute keys must be strings")
137
+ warnings .warn (
138
+ "only attribute keys of type 'string' will be allowed in the future" ,
139
+ DeprecationWarning ,
140
+ stacklevel = 2
141
+ )
142
+
143
+ try :
144
+ d_to_check = {str (k ): v for k , v in d_to_check .items ()}
145
+ except TypeError as ex : # pragma: no cover
146
+ raise TypeError ("attribute keys can not be stringified" ) from ex
147
+
148
+ if self ._version == 2 :
149
+ d = d_to_check
150
+ else :
151
+ d ["attributes" ] = d_to_check
152
+
131
153
if self ._version == 2 :
132
154
self .store [self .key ] = json_dumps (d )
133
155
if self .cache :
Original file line number Diff line number Diff line change @@ -268,3 +268,18 @@ def test_caching_off(self, zarr_version):
268
268
get_cnt = 10 if zarr_version == 2 else 12
269
269
assert get_cnt == store .counter ['__getitem__' , attrs_key ]
270
270
assert 3 == store .counter ['__setitem__' , attrs_key ]
271
+
272
+ def test_wrong_keys (self , zarr_version ):
273
+ store = _init_store (zarr_version )
274
+ a = self .init_attributes (store , zarr_version = zarr_version )
275
+
276
+ warning_msg = "only attribute keys of type 'string' will be allowed in the future"
277
+
278
+ with pytest .warns (DeprecationWarning , match = warning_msg ):
279
+ a [1 ] = "foo"
280
+
281
+ with pytest .warns (DeprecationWarning , match = warning_msg ):
282
+ a .put ({1 : "foo" })
283
+
284
+ with pytest .warns (DeprecationWarning , match = warning_msg ):
285
+ a .update ({1 : "foo" })
You can’t perform that action at this time.
0 commit comments