|
144 | 144 | from zarr.codecs.sharding import ShardingCodecIndexLocation |
145 | 145 | from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar |
146 | 146 | from zarr.storage import StoreLike |
147 | | - from zarr.types import AnyArray, AnyAsyncArray, AsyncArrayV2, AsyncArrayV3 |
| 147 | + from zarr.types import AnyArray, AnyAsyncArray, ArrayV2, ArrayV3, AsyncArrayV2, AsyncArrayV3 |
148 | 148 |
|
149 | 149 |
|
150 | 150 | # Array and AsyncArray are defined in the base ``zarr`` namespace |
@@ -300,14 +300,14 @@ class AsyncArray(Generic[T_ArrayMetadata]): |
300 | 300 | The path to the Zarr store. |
301 | 301 | codec_pipeline : CodecPipeline |
302 | 302 | The codec pipeline used for encoding and decoding chunks. |
303 | | - _config : ArrayConfig |
| 303 | + config : ArrayConfig |
304 | 304 | The runtime configuration of the array. |
305 | 305 | """ |
306 | 306 |
|
307 | 307 | metadata: T_ArrayMetadata |
308 | 308 | store_path: StorePath |
309 | 309 | codec_pipeline: CodecPipeline = field(init=False) |
310 | | - _config: ArrayConfig |
| 310 | + config: ArrayConfig |
311 | 311 |
|
312 | 312 | @overload |
313 | 313 | def __init__( |
@@ -336,7 +336,7 @@ def __init__( |
336 | 336 |
|
337 | 337 | object.__setattr__(self, "metadata", metadata_parsed) |
338 | 338 | object.__setattr__(self, "store_path", store_path) |
339 | | - object.__setattr__(self, "_config", config_parsed) |
| 339 | + object.__setattr__(self, "config", config_parsed) |
340 | 340 | object.__setattr__( |
341 | 341 | self, |
342 | 342 | "codec_pipeline", |
@@ -1012,6 +1012,11 @@ async def example(): |
1012 | 1012 | def store(self) -> Store: |
1013 | 1013 | return self.store_path.store |
1014 | 1014 |
|
| 1015 | + @property |
| 1016 | + @deprecated("Use AsyncArray.config instead.", category=ZarrDeprecationWarning) |
| 1017 | + def _config(self) -> ArrayConfig: |
| 1018 | + return self.config |
| 1019 | + |
1015 | 1020 | @property |
1016 | 1021 | def ndim(self) -> int: |
1017 | 1022 | """Returns the number of dimensions in the Array. |
@@ -1165,7 +1170,7 @@ def order(self) -> MemoryOrder: |
1165 | 1170 | if self.metadata.zarr_format == 2: |
1166 | 1171 | return self.metadata.order |
1167 | 1172 | else: |
1168 | | - return self._config.order |
| 1173 | + return self.config.order |
1169 | 1174 |
|
1170 | 1175 | @property |
1171 | 1176 | def attrs(self) -> dict[str, JSON]: |
@@ -1298,6 +1303,35 @@ def _nshards(self) -> int: |
1298 | 1303 | """ |
1299 | 1304 | return product(self._shard_grid_shape) |
1300 | 1305 |
|
| 1306 | + @overload |
| 1307 | + def with_config(self: AsyncArrayV2, config: ArrayConfigLike) -> AsyncArrayV2: ... |
| 1308 | + |
| 1309 | + @overload |
| 1310 | + def with_config(self: AsyncArrayV3, config: ArrayConfigLike) -> AsyncArrayV3: ... |
| 1311 | + |
| 1312 | + def with_config(self, config: ArrayConfigLike) -> Self: |
| 1313 | + """ |
| 1314 | + Return a copy of this Array with a new runtime configuration. |
| 1315 | +
|
| 1316 | + Parameters |
| 1317 | + ---------- |
| 1318 | +
|
| 1319 | + config : ArrayConfigLike |
| 1320 | + The runtime config for the new Array. Any keys not specified will be inherited |
| 1321 | + from the current array's config. |
| 1322 | +
|
| 1323 | + Returns |
| 1324 | + ------- |
| 1325 | + A new Array |
| 1326 | + """ |
| 1327 | + if isinstance(config, ArrayConfig): |
| 1328 | + new_config = config |
| 1329 | + else: |
| 1330 | + # Merge new config with existing config, so missing keys are inherited |
| 1331 | + # from the current array rather than from global defaults |
| 1332 | + new_config = ArrayConfig(**{**self.config.to_dict(), **config}) # type: ignore[arg-type] |
| 1333 | + return type(self)(metadata=self.metadata, store_path=self.store_path, config=new_config) |
| 1334 | + |
1301 | 1335 | async def nchunks_initialized(self) -> int: |
1302 | 1336 | """ |
1303 | 1337 | Calculate the number of chunks that have been initialized in storage. |
@@ -1570,7 +1604,7 @@ async def _get_selection( |
1570 | 1604 | ) |
1571 | 1605 | if product(indexer.shape) > 0: |
1572 | 1606 | # need to use the order from the metadata for v2 |
1573 | | - _config = self._config |
| 1607 | + _config = self.config |
1574 | 1608 | if self.metadata.zarr_format == 2: |
1575 | 1609 | _config = replace(_config, order=self.order) |
1576 | 1610 |
|
@@ -1741,7 +1775,7 @@ async def _set_selection( |
1741 | 1775 | value_buffer = prototype.nd_buffer.from_ndarray_like(value) |
1742 | 1776 |
|
1743 | 1777 | # need to use the order from the metadata for v2 |
1744 | | - _config = self._config |
| 1778 | + _config = self.config |
1745 | 1779 | if self.metadata.zarr_format == 2: |
1746 | 1780 | _config = replace(_config, order=self.metadata.order) |
1747 | 1781 |
|
@@ -2063,6 +2097,19 @@ def async_array(self) -> AsyncArray[T_ArrayMetadata]: |
2063 | 2097 | """ |
2064 | 2098 | return self._async_array |
2065 | 2099 |
|
| 2100 | + @property |
| 2101 | + def config(self) -> ArrayConfig: |
| 2102 | + """ |
| 2103 | + The runtime configuration for this array. This is a read-only property. To modify the |
| 2104 | + runtime configuration, use `Array.with_config` to create a new `Array` with the modified |
| 2105 | + configuration. |
| 2106 | +
|
| 2107 | + Returns |
| 2108 | + ------- |
| 2109 | + An `ArrayConfig` object that defines the runtime configuration for the array. |
| 2110 | + """ |
| 2111 | + return self.async_array.config |
| 2112 | + |
2066 | 2113 | @classmethod |
2067 | 2114 | @deprecated("Use zarr.create_array instead.", category=ZarrDeprecationWarning) |
2068 | 2115 | def create( |
@@ -2524,6 +2571,29 @@ def _nshards(self) -> int: |
2524 | 2571 | """ |
2525 | 2572 | return self.async_array._nshards |
2526 | 2573 |
|
| 2574 | + @overload |
| 2575 | + def with_config(self: ArrayV2, config: ArrayConfigLike) -> ArrayV2: ... |
| 2576 | + |
| 2577 | + @overload |
| 2578 | + def with_config(self: ArrayV3, config: ArrayConfigLike) -> ArrayV3: ... |
| 2579 | + |
| 2580 | + def with_config(self, config: ArrayConfigLike) -> Self: |
| 2581 | + """ |
| 2582 | + Return a copy of this Array with a new runtime configuration. |
| 2583 | +
|
| 2584 | + Parameters |
| 2585 | + ---------- |
| 2586 | +
|
| 2587 | + config : ArrayConfigLike |
| 2588 | + The runtime config for the new Array. Any keys not specified will be inherited |
| 2589 | + from the current array's config. |
| 2590 | +
|
| 2591 | + Returns |
| 2592 | + ------- |
| 2593 | + A new Array |
| 2594 | + """ |
| 2595 | + return type(self)(self._async_array.with_config(config)) |
| 2596 | + |
2527 | 2597 | @property |
2528 | 2598 | def nbytes(self) -> int: |
2529 | 2599 | """ |
|
0 commit comments