Skip to content

Commit 2a6186f

Browse files
committed
Backwards compatibility shim
1 parent b25ee98 commit 2a6186f

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/zarr/core/chunk_grids.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,28 @@ def _auto_partition(
762762
_shards_out = cast("tuple[int, ...]", shard_shape)
763763

764764
return _shards_out, _chunks_out
765+
766+
767+
# -- Backwards-compatibility shim --
768+
# Downstream packages (tifffile, cubed, ismip-indexing) import RegularChunkGrid
769+
# from this module. Emit a deprecation warning and redirect to the metadata class.
770+
771+
_DEPRECATED_NAMES = {
772+
"RegularChunkGrid": "RegularChunkGridMetadata",
773+
"RectilinearChunkGrid": "RectilinearChunkGridMetadata",
774+
}
775+
776+
777+
def __getattr__(name: str) -> Any:
778+
if name in _DEPRECATED_NAMES:
779+
new_name = _DEPRECATED_NAMES[name]
780+
import zarr.core.metadata.v3 as v3_mod
781+
782+
warnings.warn(
783+
f"Importing {name} from zarr.core.chunk_grids is deprecated. "
784+
f"Use zarr.core.metadata.v3.{new_name} instead.",
785+
DeprecationWarning,
786+
stacklevel=2,
787+
)
788+
return getattr(v3_mod, new_name)
789+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

tests/test_unified_chunk_grid.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,3 +2735,37 @@ def test_property_block_indexing_rectilinear(data: st.DataObject) -> None:
27352735
a[tuple(sel)],
27362736
err_msg=f"dim={dim}, block={block_ix}",
27372737
)
2738+
2739+
2740+
# ---------------------------------------------------------------------------
2741+
# Backwards-compatibility: import RegularChunkGrid from zarr.core.chunk_grids
2742+
# ---------------------------------------------------------------------------
2743+
# Downstream packages (tifffile, cubed, ismip-indexing) use this pattern:
2744+
# from zarr.core.chunk_grids import RegularChunkGrid
2745+
# RegularChunkGrid(chunk_shape=(...))
2746+
2747+
2748+
def test_regular_chunk_grid_import_from_chunk_grids() -> None:
2749+
"""RegularChunkGrid can be imported from zarr.core.chunk_grids with deprecation warning."""
2750+
with pytest.warns(DeprecationWarning, match="RegularChunkGrid"):
2751+
from zarr.core.chunk_grids import RegularChunkGrid
2752+
2753+
assert RegularChunkGrid is RegularChunkGridMetadata
2754+
2755+
2756+
def test_regular_chunk_grid_construction_from_chunk_grids() -> None:
2757+
"""RegularChunkGrid(chunk_shape=...) works when imported from zarr.core.chunk_grids."""
2758+
with pytest.warns(DeprecationWarning, match="RegularChunkGrid"):
2759+
from zarr.core.chunk_grids import RegularChunkGrid
2760+
2761+
grid = RegularChunkGrid(chunk_shape=(10, 20))
2762+
assert grid.chunk_shape == (10, 20)
2763+
2764+
2765+
def test_regular_chunk_grid_isinstance_from_chunk_grids() -> None:
2766+
"""isinstance check works for RegularChunkGrid imported from zarr.core.chunk_grids."""
2767+
with pytest.warns(DeprecationWarning, match="RegularChunkGrid"):
2768+
from zarr.core.chunk_grids import RegularChunkGrid
2769+
2770+
grid = RegularChunkGrid(chunk_shape=(10, 20))
2771+
assert isinstance(grid, RegularChunkGrid)

0 commit comments

Comments
 (0)