Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3741.doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The 3.0 migration guide now records that indexing a single element of an array returns a 0-dimensional `numpy.ndarray`, where Zarr-Python 2 returned a NumPy scalar, and that arrays whose shape is `()` changed in the other direction. `Array.__getitem__` follows the Python array API standard, so the guide shows `item()` and `[()]` as the ways to get a scalar back.
4 changes: 4 additions & 0 deletions changes/4669.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Reading an uncompressed sharded array into GPU memory with `FusedCodecPipeline`
no longer raises `ValueError: non-scalar numpy.ndarray cannot be used for fill`.
Under `FusedCodecPipeline`, whole uncompressed shards with structured data types
now also take the fast path.
1 change: 1 addition & 0 deletions changes/495.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`CacheStore` keeps its cache consistent with the source store when reads and writes run concurrently. A read that races a write no longer caches the value the write replaced, values larger than `max_size` are no longer written to the cache store, and failed, canceled or partly failed writes and deletions no longer leave stale entries. Canceling a write no longer stops it from reaching the source store.
15 changes: 15 additions & 0 deletions docs/user-guide/v3_migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ The following sections provide details on breaking changes in Zarr-Python 3.
`zarr.core.common.ShapeLike` input rather than separate arguments for each dimension.
Use `resize((10,10))` in place of `resize(10,10)`.

4. Indexing a single element returns a 0-dimensional array - a selection that picks out one
element returned a NumPy scalar in Zarr-Python 2. In Zarr-Python 3 it returns a
0-dimensional `numpy.ndarray`, which follows the
[Python array API standard](https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__getitem__.html),
where `__getitem__` returns an array. Arrays whose shape is `()` changed in the other
direction: reading one returned a 0-dimensional `numpy.ndarray` in Zarr-Python 2 and
returns a NumPy scalar in Zarr-Python 3. Call `item()` on the result for a Python scalar,
or index it with `()` for a NumPy scalar:

```diff
- value = arr[0] # numpy.float64
+ value = arr[0].item() # float
+ value = arr[0][()] # numpy.float64
```

### The Group class

1. Disallow direct construction - use [`zarr.open_group`][] or [`zarr.create_group`][]
Expand Down
23 changes: 1 addition & 22 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -373,30 +373,9 @@ strict = true
warn_unreachable = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool", "truthy-iterable"]

# TODO: fix the type errors in these modules and remove them from this list
[[tool.mypy.overrides]]
module = [
"tests.package_with_entrypoint.*",
"zarr.testing.stateful",
"tests.test_codecs.test_transpose",
"tests.test_config",
"tests.test_store.test_zip",
"tests.test_store.test_local",
"tests.test_store.test_memory",
"tests.test_codecs.test_codecs",
"tests.test_metadata.*",
"tests.test_store.test_core",
"tests.test_store.test_logging",
"tests.test_store.test_object",
"tests.test_store.test_stateful",
"tests.test_store.test_wrapper",
]
strict = false

# TODO: Move the next modules up to the strict = false section
# and fix the errors
[[tool.mypy.overrides]]
module = [
"tests.test_store.test_fsspec",
"tests.test_group",
"tests.test_indexing",
"tests.test_properties",
Expand Down
32 changes: 19 additions & 13 deletions src/zarr/codecs/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from zarr.abc.store import ByteGetter
from zarr.core.array_spec import ArraySpec
from zarr.core.buffer import Buffer, NDBuffer
from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar, ZDType
from zarr.core.indexing import Selector, SelectorTuple


Expand Down Expand Up @@ -100,25 +101,30 @@ def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self:
)
return self

def _stored_dtype(self, dtype: ZDType[TBaseDType, TBaseScalar]) -> np.dtype[Any]:
"""The NumPy data type to view stored bytes with.
The byte order of the stored data is set by this codec's `endian`
configuration; the byte order of the decoded array is set by the array's
data type. The two are independent: the raw bytes are viewed with a dtype
in the stored byte order, then converted to the declared dtype if needed.
"""
if isinstance(dtype, HasEndianness):
return replace(dtype, endianness=self.endian).to_native_dtype() # type: ignore[call-arg]
native = dtype.to_native_dtype()
if isinstance(dtype, Struct) and self.endian is not None:
# Per the struct data type spec, all multi-byte fields are stored in the
# byte order configured on this codec.
return native.newbyteorder(self.endian)
return native

def _decode_sync(
self,
chunk_bytes: Buffer,
chunk_spec: ArraySpec,
) -> NDBuffer:
endian_str = self.endian
dtype = chunk_spec.dtype.to_native_dtype()
# The byte order of the stored data is set by this codec's `endian`
# configuration; the byte order of the decoded array is set by the array's
# data type. The two are independent: the raw bytes are viewed with a dtype
# in the stored byte order, then converted to the declared dtype if needed.
if isinstance(chunk_spec.dtype, HasEndianness):
view_dtype = replace(chunk_spec.dtype, endianness=endian_str).to_native_dtype() # type: ignore[call-arg]
elif isinstance(chunk_spec.dtype, Struct) and endian_str is not None:
# Per the struct data type spec, all multi-byte fields are stored in the
# byte order configured on this codec.
view_dtype = dtype.newbyteorder(endian_str)
else:
view_dtype = dtype
view_dtype = self._stored_dtype(chunk_spec.dtype)
as_array_like = chunk_bytes.as_array_like()
chunk_array = chunk_spec.prototype.nd_buffer.from_ndarray_like(
as_array_like.view(dtype=view_dtype) # type: ignore[attr-defined]
Expand Down
34 changes: 10 additions & 24 deletions src/zarr/codecs/sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@
product,
)
from zarr.core.config import config as zarr_config
from zarr.core.dtype.common import HasEndianness
from zarr.core.dtype.npy.int import UInt64
from zarr.core.dtype.npy.structured import Struct
from zarr.core.indexing import (
BasicIndexer,
ChunkProjection,
Expand Down Expand Up @@ -1187,8 +1185,6 @@ def _decode_full_shard_bulk_if_uncompressed(
dtype/endian view with no reordering. A trailing crc32c is NOT accepted
(the bulk path can't verify per-chunk checksums, so crc shards keep the
per-chunk path's corruption detection);
- the data type is not structured (the byte-order handling below has no
`Struct` branch);
- `indexer` is an identity full-shard read (`_is_identity_full_read`);
- the stored index is dense (every chunk present, equal fixed length,
exactly tiling the data section) so the data section is a regular
Expand All @@ -1197,7 +1193,8 @@ def _decode_full_shard_bulk_if_uncompressed(
Chunk positions are read from the stored index, so this is correct for
any `subchunk_write_order` (morton / lexicographic / colexicographic /
unordered). The on-disk byte order is taken from the BytesCodec's
`endian`, so big- and little-endian shards both decode correctly.
`endian` (`BytesCodec._stored_dtype`), so big- and little-endian shards,
structured ones included, decode as they do on the per-chunk path.
"""
# --- gate on a trivial, fixed-size inner codec chain ---
if not self._inner_codecs_fixed_size:
Expand All @@ -1212,12 +1209,6 @@ def _decode_full_shard_bulk_if_uncompressed(
return None
ab_codec = self.codecs[0]

# The byte-order handling below lacks the structured-dtype branch of
# `BytesCodec._decode_sync` (which applies `newbyteorder` to multi-byte
# struct fields), so structured dtypes must take the per-chunk path.
if isinstance(shard_spec.dtype, Struct):
return None

chunks_per_shard = self._get_chunks_per_shard(shard_spec)
chunk_spec = self._get_chunk_spec(shard_spec)
n_chunks = product(chunks_per_shard)
Expand Down Expand Up @@ -1257,21 +1248,16 @@ def _decode_full_shard_bulk_if_uncompressed(
# a dense, crc-free, fixed-size layout the payload length is exactly the
# encoded item-bytes of one chunk.
native_dtype = shard_spec.dtype.to_native_dtype()
raw = shard_bytes.as_numpy_array().view(np.uint8)
# `as_array_like`, not `as_numpy_array`: the payloads must stay in the
# buffer's own memory (a CuPy array under `zarr.config.enable_gpu()`), or
# assigning them into a device `out` below fails.
raw = shard_bytes.as_array_like()
payload = chunk_byte_length
cs = self.chunk_shape

# On-disk byte order is carried by the BytesCodec's `endian`, NOT by the
# data type (zarr v3). Build the read-view dtype from the codec's endian
# exactly as BytesCodec._decode_sync does, so a big-endian shard read on a
# little-endian host (or vice versa) is interpreted correctly. Assigning
# into the native-dtype `out` then performs any needed byteswap. `endian`
# is now a plain Literal['little', 'big'] | None string (no longer an enum).
endian_str = ab_codec.endian
if isinstance(chunk_spec.dtype, HasEndianness):
stored_dtype = replace(chunk_spec.dtype, endianness=endian_str).to_native_dtype() # type: ignore[call-arg]
else:
stored_dtype = chunk_spec.dtype.to_native_dtype()
# data type (zarr v3). Assigning the stored-order view into the
# native-dtype `out` performs any needed byteswap.
stored_dtype = ab_codec._stored_dtype(chunk_spec.dtype)

offsets = index.offsets_and_lengths[..., 0].reshape(-1) # localized coords, C-order
coords_c = list(np.ndindex(chunks_per_shard))
Expand All @@ -1280,7 +1266,7 @@ def _decode_full_shard_bulk_if_uncompressed(
)
for flat, coord in enumerate(coords_c):
start = int(offsets[flat])
chunk = raw[start : start + payload].view(stored_dtype).reshape(cs)
chunk = raw[start : start + payload].view(stored_dtype).reshape(cs) # type: ignore[attr-defined]
sel = tuple(slice(c * s, c * s + s) for c, s in zip(coord, cs, strict=True))
out[sel] = chunk
return out
Expand Down
Loading