3030 RuleBasedStateMachine ,
3131 initialize ,
3232 invariant ,
33- precondition ,
3433 rule ,
3534)
3635
@@ -79,8 +78,8 @@ def __init__(self) -> None:
7978 self .fill = 0
8079 # What the store holds, indexed like the array and extending past its shape.
8180 self .stored : np .ndarray [Any , np .dtype [np .int16 ]] = np .zeros ((0 ,), dtype = DTYPE )
82- # A store with an invalid stored chunk size warns until its metadata is re-saved.
83- self .expect_open_warning = False
81+ # Axes stored with chunk size 0; the store warns until its metadata is re-saved.
82+ self .legacy_axes : list [ int ] = []
8483
8584 # -------------------------------------------------------------- creation
8685 @initialize (data = st .data ())
@@ -130,13 +129,12 @@ def create(self, data: st.DataObject) -> None:
130129 # A stored chunk size of 0, as zarr-python wrote for arrays created with a
131130 # zero-length axis; older releases could then grow the axis without storing
132131 # a chunk, so any extent is possible. Sharded arrays store it in the outer grid.
133- zero_axes = data .draw (
132+ self . legacy_axes = data .draw (
134133 st .lists (st .integers (0 , len (shape ) - 1 ), min_size = 1 , unique = True ),
135134 label = "axes stored with chunk size 0" ,
136135 )
137136 stored_zero = data .draw (st .sampled_from ([0 , False ]), label = "stored zero" )
138- self ._rewrite_stored_chunks (zarr_format , zero_axes , stored_zero )
139- self .expect_open_warning = True
137+ self ._rewrite_stored_chunks (zarr_format , self .legacy_axes , stored_zero )
140138 event ("legacy zero chunk size" )
141139
142140 def _rewrite_stored_chunks (
@@ -158,7 +156,7 @@ def _open(self) -> zarr.Array[Any]:
158156 warnings .simplefilter ("always" , ZarrUserWarning )
159157 arr = zarr .open_array (self .store , path = self .path , mode = "r+" )
160158 warned = any (issubclass (w .category , ZarrUserWarning ) for w in record )
161- assert warned is self .expect_open_warning , [str (w .message ) for w in record ]
159+ assert warned is bool ( self .legacy_axes ) , [str (w .message ) for w in record ]
162160 return arr
163161
164162 # ----------------------------------------------------------------- model
@@ -209,13 +207,19 @@ def _model_write(self, arr: zarr.Array[Any], region: tuple[slice, ...], values:
209207 @rule (data = st .data ())
210208 def append (self , data : st .DataObject ) -> None :
211209 arr = self ._open ()
212- axis = data .draw (st .integers (0 , len (self .shape ) - 1 ), label = "axis" )
210+ axes = st .integers (0 , len (self .shape ) - 1 )
211+ if self .legacy_axes :
212+ # What a user of an older release did next: grow an axis stored with chunk size 0.
213+ axes = st .sampled_from (self .legacy_axes ) | axes
214+ axis = data .draw (axes , label = "axis" )
213215 block_shape = list (self .shape )
214216 block_shape [axis ] = data .draw (st .integers (0 , 4 ), label = "rows" )
215217 block = data .draw (npst .arrays (DTYPE , tuple (block_shape )), label = "block" )
216218 note (f"append { block .shape } along { axis } to { self .shape } " )
217219 if self .shape [axis ] == 0 and block .shape [axis ]:
218220 event ("append to a zero-length axis" )
221+ if axis in self .legacy_axes and block .shape [axis ]:
222+ event ("grow an axis stored with chunk size 0" )
219223 old_extent = self .shape [axis ]
220224 arr .append (block , axis = axis )
221225 self ._model_resize (ChunkGrid .from_metadata (arr .metadata ), arr .shape )
@@ -224,7 +228,7 @@ def append(self, data: st.DataObject) -> None:
224228 )
225229 self ._model_write (arr , region , block )
226230 # Growing the array rewrites its metadata, which stores any correction.
227- self .expect_open_warning = False
231+ self .legacy_axes = []
228232
229233 @rule (data = st .data ())
230234 def resize (self , data : st .DataObject ) -> None :
@@ -233,10 +237,12 @@ def resize(self, data: st.DataObject) -> None:
233237 st .tuples (* (st .integers (0 , MAX_SIDE ) for _ in self .shape )), label = "new shape"
234238 )
235239 note (f"resize { self .shape } -> { new_shape } " )
240+ if any (new_shape [axis ] > self .shape [axis ] for axis in self .legacy_axes ):
241+ event ("grow an axis stored with chunk size 0" )
236242 grid = ChunkGrid .from_metadata (arr .metadata )
237243 arr .resize (new_shape )
238244 self ._model_resize (grid , new_shape )
239- self .expect_open_warning = False
245+ self .legacy_axes = []
240246
241247 @rule (data = st .data ())
242248 def write (self , data : st .DataObject ) -> None :
@@ -251,12 +257,15 @@ def write(self, data: st.DataObject) -> None:
251257 arr [region ] = values
252258 self ._model_write (arr , region , values )
253259
254- @precondition (lambda self : self .expect_open_warning )
255260 @rule ()
256261 def resave_metadata (self ) -> None :
257- """What the warning for an invalid stored chunk size tells users to do."""
258- self ._open ().update_attributes ({})
259- self .expect_open_warning = False
262+ """What the warning for an invalid stored chunk size tells users to do. It stores
263+ the metadata as read, which is a no-op for valid metadata."""
264+ arr = self ._open ()
265+ read = arr .metadata
266+ arr .update_attributes ({})
267+ self .legacy_axes = []
268+ assert self ._open ().metadata == read
260269
261270 def teardown (self ) -> None :
262271 self ._rectilinear .__exit__ (None , None , None )
0 commit comments