|
141 | 141 | from zarr.codecs.sharding import ShardingCodecIndexLocation |
142 | 142 | from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar |
143 | 143 | from zarr.storage import StoreLike |
144 | | - from zarr.types import AnyArray, AnyAsyncArray, AsyncArrayV2, AsyncArrayV3 |
| 144 | + from zarr.types import AnyArray, AnyAsyncArray, ArrayV2, ArrayV3, AsyncArrayV2, AsyncArrayV3 |
145 | 145 |
|
146 | 146 |
|
147 | 147 | # Array and AsyncArray are defined in the base ``zarr`` namespace |
@@ -297,14 +297,14 @@ class AsyncArray[T_ArrayMetadata: (ArrayV2Metadata, ArrayV3Metadata)]: |
297 | 297 | The path to the Zarr store. |
298 | 298 | codec_pipeline : CodecPipeline |
299 | 299 | The codec pipeline used for encoding and decoding chunks. |
300 | | - _config : ArrayConfig |
| 300 | + config : ArrayConfig |
301 | 301 | The runtime configuration of the array. |
302 | 302 | """ |
303 | 303 |
|
304 | 304 | metadata: T_ArrayMetadata |
305 | 305 | store_path: StorePath |
306 | 306 | codec_pipeline: CodecPipeline = field(init=False) |
307 | | - _config: ArrayConfig |
| 307 | + config: ArrayConfig |
308 | 308 |
|
309 | 309 | @overload |
310 | 310 | def __init__( |
@@ -333,7 +333,7 @@ def __init__( |
333 | 333 |
|
334 | 334 | object.__setattr__(self, "metadata", metadata_parsed) |
335 | 335 | object.__setattr__(self, "store_path", store_path) |
336 | | - object.__setattr__(self, "_config", config_parsed) |
| 336 | + object.__setattr__(self, "config", config_parsed) |
337 | 337 | object.__setattr__( |
338 | 338 | self, |
339 | 339 | "codec_pipeline", |
@@ -1009,6 +1009,11 @@ async def example(): |
1009 | 1009 | def store(self) -> Store: |
1010 | 1010 | return self.store_path.store |
1011 | 1011 |
|
| 1012 | + @property |
| 1013 | + @deprecated("Use AsyncArray.config instead.", category=ZarrDeprecationWarning) |
| 1014 | + def _config(self) -> ArrayConfig: |
| 1015 | + return self.config |
| 1016 | + |
1012 | 1017 | @property |
1013 | 1018 | def ndim(self) -> int: |
1014 | 1019 | """Returns the number of dimensions in the Array. |
@@ -1162,7 +1167,7 @@ def order(self) -> MemoryOrder: |
1162 | 1167 | if self.metadata.zarr_format == 2: |
1163 | 1168 | return self.metadata.order |
1164 | 1169 | else: |
1165 | | - return self._config.order |
| 1170 | + return self.config.order |
1166 | 1171 |
|
1167 | 1172 | @property |
1168 | 1173 | def attrs(self) -> dict[str, JSON]: |
@@ -1295,6 +1300,35 @@ def _nshards(self) -> int: |
1295 | 1300 | """ |
1296 | 1301 | return product(self._shard_grid_shape) |
1297 | 1302 |
|
| 1303 | + @overload |
| 1304 | + def with_config(self: AsyncArrayV2, config: ArrayConfigLike) -> AsyncArrayV2: ... |
| 1305 | + |
| 1306 | + @overload |
| 1307 | + def with_config(self: AsyncArrayV3, config: ArrayConfigLike) -> AsyncArrayV3: ... |
| 1308 | + |
| 1309 | + def with_config(self, config: ArrayConfigLike) -> Self: |
| 1310 | + """ |
| 1311 | + Return a copy of this Array with a new runtime configuration. |
| 1312 | +
|
| 1313 | + Parameters |
| 1314 | + ---------- |
| 1315 | +
|
| 1316 | + config : ArrayConfigLike |
| 1317 | + The runtime config for the new Array. Any keys not specified will be inherited |
| 1318 | + from the current array's config. |
| 1319 | +
|
| 1320 | + Returns |
| 1321 | + ------- |
| 1322 | + A new Array |
| 1323 | + """ |
| 1324 | + if isinstance(config, ArrayConfig): |
| 1325 | + new_config = config |
| 1326 | + else: |
| 1327 | + # Merge new config with existing config, so missing keys are inherited |
| 1328 | + # from the current array rather than from global defaults |
| 1329 | + new_config = ArrayConfig(**{**self.config.to_dict(), **config}) # type: ignore[arg-type] |
| 1330 | + return type(self)(metadata=self.metadata, store_path=self.store_path, config=new_config) |
| 1331 | + |
1298 | 1332 | async def nchunks_initialized(self) -> int: |
1299 | 1333 | """ |
1300 | 1334 | Calculate the number of chunks that have been initialized in storage. |
@@ -1567,7 +1601,7 @@ async def _get_selection( |
1567 | 1601 | ) |
1568 | 1602 | if product(indexer.shape) > 0: |
1569 | 1603 | # need to use the order from the metadata for v2 |
1570 | | - _config = self._config |
| 1604 | + _config = self.config |
1571 | 1605 | if self.metadata.zarr_format == 2: |
1572 | 1606 | _config = replace(_config, order=self.order) |
1573 | 1607 |
|
@@ -1738,7 +1772,7 @@ async def _set_selection( |
1738 | 1772 | value_buffer = prototype.nd_buffer.from_ndarray_like(value) |
1739 | 1773 |
|
1740 | 1774 | # need to use the order from the metadata for v2 |
1741 | | - _config = self._config |
| 1775 | + _config = self.config |
1742 | 1776 | if self.metadata.zarr_format == 2: |
1743 | 1777 | _config = replace(_config, order=self.metadata.order) |
1744 | 1778 |
|
@@ -2060,6 +2094,19 @@ def async_array(self) -> AsyncArray[T_ArrayMetadata]: |
2060 | 2094 | """ |
2061 | 2095 | return self._async_array |
2062 | 2096 |
|
| 2097 | + @property |
| 2098 | + def config(self) -> ArrayConfig: |
| 2099 | + """ |
| 2100 | + The runtime configuration for this array. This is a read-only property. To modify the |
| 2101 | + runtime configuration, use `Array.with_config` to create a new `Array` with the modified |
| 2102 | + configuration. |
| 2103 | +
|
| 2104 | + Returns |
| 2105 | + ------- |
| 2106 | + An `ArrayConfig` object that defines the runtime configuration for the array. |
| 2107 | + """ |
| 2108 | + return self.async_array.config |
| 2109 | + |
2063 | 2110 | @classmethod |
2064 | 2111 | @deprecated("Use zarr.create_array instead.", category=ZarrDeprecationWarning) |
2065 | 2112 | def create( |
@@ -2521,6 +2568,29 @@ def _nshards(self) -> int: |
2521 | 2568 | """ |
2522 | 2569 | return self.async_array._nshards |
2523 | 2570 |
|
| 2571 | + @overload |
| 2572 | + def with_config(self: ArrayV2, config: ArrayConfigLike) -> ArrayV2: ... |
| 2573 | + |
| 2574 | + @overload |
| 2575 | + def with_config(self: ArrayV3, config: ArrayConfigLike) -> ArrayV3: ... |
| 2576 | + |
| 2577 | + def with_config(self, config: ArrayConfigLike) -> Self: |
| 2578 | + """ |
| 2579 | + Return a copy of this Array with a new runtime configuration. |
| 2580 | +
|
| 2581 | + Parameters |
| 2582 | + ---------- |
| 2583 | +
|
| 2584 | + config : ArrayConfigLike |
| 2585 | + The runtime config for the new Array. Any keys not specified will be inherited |
| 2586 | + from the current array's config. |
| 2587 | +
|
| 2588 | + Returns |
| 2589 | + ------- |
| 2590 | + A new Array |
| 2591 | + """ |
| 2592 | + return type(self)(self._async_array.with_config(config)) |
| 2593 | + |
2524 | 2594 | @property |
2525 | 2595 | def nbytes(self) -> int: |
2526 | 2596 | """ |
|
0 commit comments