|
152 | 152 | from zarr.codecs.sharding import ShardingCodecIndexLocation |
153 | 153 | from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar |
154 | 154 | from zarr.storage import StoreLike |
155 | | - from zarr.types import AnyArray, AnyAsyncArray, AsyncArrayV2, AsyncArrayV3 |
| 155 | + from zarr.types import AnyArray, AnyAsyncArray, ArrayV2, ArrayV3, AsyncArrayV2, AsyncArrayV3 |
156 | 156 |
|
157 | 157 |
|
158 | 158 | # Array and AsyncArray are defined in the base ``zarr`` namespace |
@@ -308,14 +308,14 @@ class AsyncArray(Generic[T_ArrayMetadata]): |
308 | 308 | The path to the Zarr store. |
309 | 309 | codec_pipeline : CodecPipeline |
310 | 310 | The codec pipeline used for encoding and decoding chunks. |
311 | | - _config : ArrayConfig |
| 311 | + config : ArrayConfig |
312 | 312 | The runtime configuration of the array. |
313 | 313 | """ |
314 | 314 |
|
315 | 315 | metadata: T_ArrayMetadata |
316 | 316 | store_path: StorePath |
317 | 317 | codec_pipeline: CodecPipeline = field(init=False) |
318 | | - _config: ArrayConfig |
| 318 | + config: ArrayConfig |
319 | 319 |
|
320 | 320 | @overload |
321 | 321 | def __init__( |
@@ -344,7 +344,7 @@ def __init__( |
344 | 344 |
|
345 | 345 | object.__setattr__(self, "metadata", metadata_parsed) |
346 | 346 | object.__setattr__(self, "store_path", store_path) |
347 | | - object.__setattr__(self, "_config", config_parsed) |
| 347 | + object.__setattr__(self, "config", config_parsed) |
348 | 348 | object.__setattr__( |
349 | 349 | self, |
350 | 350 | "codec_pipeline", |
@@ -1028,6 +1028,11 @@ async def example(): |
1028 | 1028 | def store(self) -> Store: |
1029 | 1029 | return self.store_path.store |
1030 | 1030 |
|
| 1031 | + @property |
| 1032 | + @deprecated("Use AsyncArray.config instead.", category=ZarrDeprecationWarning) |
| 1033 | + def _config(self) -> ArrayConfig: |
| 1034 | + return self.config |
| 1035 | + |
1031 | 1036 | @property |
1032 | 1037 | def ndim(self) -> int: |
1033 | 1038 | """Returns the number of dimensions in the Array. |
@@ -1215,7 +1220,7 @@ def order(self) -> MemoryOrder: |
1215 | 1220 | if self.metadata.zarr_format == 2: |
1216 | 1221 | return self.metadata.order |
1217 | 1222 | else: |
1218 | | - return self._config.order |
| 1223 | + return self.config.order |
1219 | 1224 |
|
1220 | 1225 | @property |
1221 | 1226 | def attrs(self) -> dict[str, JSON]: |
@@ -1366,6 +1371,35 @@ def _nshards(self) -> int: |
1366 | 1371 | """ |
1367 | 1372 | return product(self._shard_grid_shape) |
1368 | 1373 |
|
| 1374 | + @overload |
| 1375 | + def with_config(self: AsyncArrayV2, config: ArrayConfigLike) -> AsyncArrayV2: ... |
| 1376 | + |
| 1377 | + @overload |
| 1378 | + def with_config(self: AsyncArrayV3, config: ArrayConfigLike) -> AsyncArrayV3: ... |
| 1379 | + |
| 1380 | + def with_config(self, config: ArrayConfigLike) -> Self: |
| 1381 | + """ |
| 1382 | + Return a copy of this Array with a new runtime configuration. |
| 1383 | +
|
| 1384 | + Parameters |
| 1385 | + ---------- |
| 1386 | +
|
| 1387 | + config : ArrayConfigLike |
| 1388 | + The runtime config for the new Array. Any keys not specified will be inherited |
| 1389 | + from the current array's config. |
| 1390 | +
|
| 1391 | + Returns |
| 1392 | + ------- |
| 1393 | + A new Array |
| 1394 | + """ |
| 1395 | + if isinstance(config, ArrayConfig): |
| 1396 | + new_config = config |
| 1397 | + else: |
| 1398 | + # Merge new config with existing config, so missing keys are inherited |
| 1399 | + # from the current array rather than from global defaults |
| 1400 | + new_config = ArrayConfig(**{**self.config.to_dict(), **config}) # type: ignore[arg-type] |
| 1401 | + return type(self)(metadata=self.metadata, store_path=self.store_path, config=new_config) |
| 1402 | + |
1369 | 1403 | async def nchunks_initialized(self) -> int: |
1370 | 1404 | """ |
1371 | 1405 | Calculate the number of chunks that have been initialized in storage. |
@@ -1641,7 +1675,7 @@ async def _get_selection( |
1641 | 1675 | ) |
1642 | 1676 | if product(indexer.shape) > 0: |
1643 | 1677 | # need to use the order from the metadata for v2 |
1644 | | - _config = self._config |
| 1678 | + _config = self.config |
1645 | 1679 | if self.metadata.zarr_format == 2: |
1646 | 1680 | _config = replace(_config, order=self.order) |
1647 | 1681 |
|
@@ -1812,7 +1846,7 @@ async def _set_selection( |
1812 | 1846 | value_buffer = prototype.nd_buffer.from_ndarray_like(value) |
1813 | 1847 |
|
1814 | 1848 | # need to use the order from the metadata for v2 |
1815 | | - _config = self._config |
| 1849 | + _config = self.config |
1816 | 1850 | if self.metadata.zarr_format == 2: |
1817 | 1851 | _config = replace(_config, order=self.metadata.order) |
1818 | 1852 |
|
@@ -2134,6 +2168,19 @@ def async_array(self) -> AsyncArray[T_ArrayMetadata]: |
2134 | 2168 | """ |
2135 | 2169 | return self._async_array |
2136 | 2170 |
|
| 2171 | + @property |
| 2172 | + def config(self) -> ArrayConfig: |
| 2173 | + """ |
| 2174 | + The runtime configuration for this array. This is a read-only property. To modify the |
| 2175 | + runtime configuration, use `Array.with_config` to create a new `Array` with the modified |
| 2176 | + configuration. |
| 2177 | +
|
| 2178 | + Returns |
| 2179 | + ------- |
| 2180 | + An `ArrayConfig` object that defines the runtime configuration for the array. |
| 2181 | + """ |
| 2182 | + return self.async_array.config |
| 2183 | + |
2137 | 2184 | @classmethod |
2138 | 2185 | @deprecated("Use zarr.create_array instead.", category=ZarrDeprecationWarning) |
2139 | 2186 | def create( |
@@ -2617,6 +2664,29 @@ def _nshards(self) -> int: |
2617 | 2664 | """ |
2618 | 2665 | return self.async_array._nshards |
2619 | 2666 |
|
| 2667 | + @overload |
| 2668 | + def with_config(self: ArrayV2, config: ArrayConfigLike) -> ArrayV2: ... |
| 2669 | + |
| 2670 | + @overload |
| 2671 | + def with_config(self: ArrayV3, config: ArrayConfigLike) -> ArrayV3: ... |
| 2672 | + |
| 2673 | + def with_config(self, config: ArrayConfigLike) -> Self: |
| 2674 | + """ |
| 2675 | + Return a copy of this Array with a new runtime configuration. |
| 2676 | +
|
| 2677 | + Parameters |
| 2678 | + ---------- |
| 2679 | +
|
| 2680 | + config : ArrayConfigLike |
| 2681 | + The runtime config for the new Array. Any keys not specified will be inherited |
| 2682 | + from the current array's config. |
| 2683 | +
|
| 2684 | + Returns |
| 2685 | + ------- |
| 2686 | + A new Array |
| 2687 | + """ |
| 2688 | + return type(self)(self._async_array.with_config(config)) |
| 2689 | + |
2620 | 2690 | @property |
2621 | 2691 | def nbytes(self) -> int: |
2622 | 2692 | """ |
|
0 commit comments