@@ -781,6 +781,73 @@ def test_resize_2d(store: MemoryStore, zarr_format: ZarrFormat) -> None:
781781 assert new_shape == result .shape
782782
783783
784+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
785+ def test_resize_growing_skips_chunk_enumeration (
786+ store : MemoryStore , zarr_format : ZarrFormat
787+ ) -> None :
788+ """Growing an array should not enumerate chunk coords for deletion (#3650 mitigation)."""
789+ from zarr .core .chunk_grids import RegularChunkGrid
790+
791+ z = zarr .create (
792+ shape = (10 , 10 ),
793+ chunks = (5 , 5 ),
794+ dtype = "i4" ,
795+ fill_value = 0 ,
796+ store = store ,
797+ zarr_format = zarr_format ,
798+ )
799+ z [:] = np .ones ((10 , 10 ), dtype = "i4" )
800+
801+ # growth only - ensure no chunk coords are enumerated
802+ with mock .patch .object (
803+ RegularChunkGrid ,
804+ "all_chunk_coords" ,
805+ wraps = z .metadata .chunk_grid .all_chunk_coords ,
806+ ) as mock_coords :
807+ z .resize ((20 , 20 ))
808+ mock_coords .assert_not_called ()
809+
810+ assert z .shape == (20 , 20 )
811+ np .testing .assert_array_equal (np .ones ((10 , 10 ), dtype = "i4" ), z [:10 , :10 ])
812+ np .testing .assert_array_equal (np .zeros ((10 , 10 ), dtype = "i4" ), z [10 :, 10 :])
813+
814+ # shrink - ensure no regression of behaviour
815+ with mock .patch .object (
816+ RegularChunkGrid ,
817+ "all_chunk_coords" ,
818+ wraps = z .metadata .chunk_grid .all_chunk_coords ,
819+ ) as mock_coords :
820+ z .resize ((5 , 5 ))
821+ assert mock_coords .call_count > 0
822+
823+ assert z .shape == (5 , 5 )
824+ np .testing .assert_array_equal (np .ones ((5 , 5 ), dtype = "i4" ), z [:])
825+
826+ # mixed: grow dim 0, shrink dim 1 - ensure deletion path runs
827+ z2 = zarr .create (
828+ shape = (10 , 10 ),
829+ chunks = (5 , 5 ),
830+ dtype = "i4" ,
831+ fill_value = 0 ,
832+ store = store ,
833+ zarr_format = zarr_format ,
834+ overwrite = True ,
835+ )
836+ z2 [:] = np .ones ((10 , 10 ), dtype = "i4" )
837+
838+ with mock .patch .object (
839+ RegularChunkGrid ,
840+ "all_chunk_coords" ,
841+ wraps = z2 .metadata .chunk_grid .all_chunk_coords ,
842+ ) as mock_coords :
843+ z2 .resize ((20 , 5 ))
844+ assert mock_coords .call_count > 0
845+
846+ assert z2 .shape == (20 , 5 )
847+ np .testing .assert_array_equal (np .ones ((10 , 5 ), dtype = "i4" ), z2 [:10 , :])
848+ np .testing .assert_array_equal (np .zeros ((10 , 5 ), dtype = "i4" ), z2 [10 :, :])
849+
850+
784851@pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
785852def test_append_1d (store : MemoryStore , zarr_format : ZarrFormat ) -> None :
786853 a = np .arange (105 )
0 commit comments