|
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. |
@@ -1926,6 +1960,19 @@ def async_array(self) -> AsyncArray[T_ArrayMetadata]: |
1926 | 1960 | """ |
1927 | 1961 | return self._async_array |
1928 | 1962 |
|
| 1963 | + @property |
| 1964 | + def config(self) -> ArrayConfig: |
| 1965 | + """ |
| 1966 | + The runtime configuration for this array. This is a read-only property. To modify the |
| 1967 | + runtime configuration, use `Array.with_config` to create a new `Array` with the modified |
| 1968 | + configuration. |
| 1969 | +
|
| 1970 | + Returns |
| 1971 | + ------- |
| 1972 | + An `ArrayConfig` object that defines the runtime configuration for the array. |
| 1973 | + """ |
| 1974 | + return self.async_array.config |
| 1975 | + |
1929 | 1976 | @classmethod |
1930 | 1977 | @deprecated("Use zarr.create_array instead.", category=ZarrDeprecationWarning) |
1931 | 1978 | def create( |
@@ -2387,6 +2434,29 @@ def _nshards(self) -> int: |
2387 | 2434 | """ |
2388 | 2435 | return self.async_array._nshards |
2389 | 2436 |
|
| 2437 | + @overload |
| 2438 | + def with_config(self: ArrayV2, config: ArrayConfigLike) -> ArrayV2: ... |
| 2439 | + |
| 2440 | + @overload |
| 2441 | + def with_config(self: ArrayV3, config: ArrayConfigLike) -> ArrayV3: ... |
| 2442 | + |
| 2443 | + def with_config(self, config: ArrayConfigLike) -> Self: |
| 2444 | + """ |
| 2445 | + Return a copy of this Array with a new runtime configuration. |
| 2446 | +
|
| 2447 | + Parameters |
| 2448 | + ---------- |
| 2449 | +
|
| 2450 | + config : ArrayConfigLike |
| 2451 | + The runtime config for the new Array. Any keys not specified will be inherited |
| 2452 | + from the current array's config. |
| 2453 | +
|
| 2454 | + Returns |
| 2455 | + ------- |
| 2456 | + A new Array |
| 2457 | + """ |
| 2458 | + return type(self)(self._async_array.with_config(config)) |
| 2459 | + |
2390 | 2460 | @property |
2391 | 2461 | def nbytes(self) -> int: |
2392 | 2462 | """ |
|
0 commit comments