Skip to content
Open
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
22 changes: 21 additions & 1 deletion src/zarr/core/dtype/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,27 @@ def _check_native_dtype(cls: type[Self], dtype: TBaseDType) -> TypeGuard[DType]:
Bool
True if the dtype matches, False otherwise.
"""
return type(dtype) is cls.dtype_cls
# Keep the exact class check in a variable: narrowing on ``type(dtype) is ...``
# makes mypy treat the layout fallback below as unreachable.
exact_match = type(dtype) is cls.dtype_cls
if exact_match:
return True
# NumPy's C type spellings do not always map to the canonical dtype class:
# ``np.dtype("q")`` is a ``LongLongDType`` instance rather than an
# ``Int64DType`` instance, even though the two describe the same memory
# layout. The width of ``long long`` depends on the platform NumPy was
# built for, so the dtype class alone is not a reliable test. Fall back to
# comparing the layout (kind and item size), which is what a Zarr data type
# actually encodes.
try:
# numpy's stubs only type ``dtype(...)`` with an argument; the concrete
# dtype classes (``np.dtypes.Int64DType`` etc.) construct without one.
expected: TBaseDType = cls.dtype_cls() # type: ignore[call-overload]
except TypeError:
# Flexible/parametric dtypes (e.g. ``VoidDType``) have no fixed layout
# to compare against, so the exact dtype class check above stands.
return False
return bool(dtype.kind == expected.kind and dtype.itemsize == expected.itemsize)

@classmethod
@abstractmethod
Expand Down
11 changes: 9 additions & 2 deletions tests/test_dtype/test_npy/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ class TestInt32(BaseTestZDType):
class TestInt64(BaseTestZDType):
test_cls = Int64
scalar_type = np.int64
valid_dtype = (np.dtype(">i8"), np.dtype("<i8"))
# np.dtype("q") is a np.dtypes.LongLongDType instance, not a np.dtypes.Int64DType
# instance, even though "long long" is 64-bit on this platform. It describes the
# same layout, so it is a valid Int64. See issue #3282.
valid_dtype = (np.dtype(">i8"), np.dtype("<i8"), np.dtype("q"))
invalid_dtype = (
np.dtype(np.int8),
np.dtype(np.uint16),
Expand Down Expand Up @@ -267,7 +270,11 @@ class TestUInt32(BaseTestZDType):
class TestUInt64(BaseTestZDType):
test_cls = UInt64
scalar_type = np.uint64
valid_dtype = (np.dtype(">u8"), np.dtype("<u8"))
# np.dtype("Q") is a np.dtypes.ULongLongDType instance, not a
# np.dtypes.UInt64DType instance, even though "unsigned long long" is 64-bit on
# this platform. It describes the same layout, so it is a valid UInt64.
# See issue #3282.
valid_dtype = (np.dtype(">u8"), np.dtype("<u8"), np.dtype("Q"))
invalid_dtype = (
np.dtype(np.int8),
np.dtype(np.int16),
Expand Down
18 changes: 18 additions & 0 deletions tests/test_dtype_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
FixedLengthUTF32,
Int8,
Int16,
Int64,
RawBytes,
Struct,
UInt8,
UInt64,
VariableLengthUTF8,
ZDType,
data_type_registry,
Expand Down Expand Up @@ -91,6 +93,22 @@ def test_match_dtype_string_na_object_error(
with pytest.raises(ValueError, match=r"Zarr data type resolution from StringDType.*failed"):
data_type_registry_fixture.match_dtype(dtype)

@staticmethod
@pytest.mark.parametrize(("dtype_str", "expected_cls"), [("q", Int64), ("Q", UInt64)])
def test_match_dtype_c_type_spelling(dtype_str: str, expected_cls: type) -> None:
"""
Test that the C type-name spellings resolve through the registry.

``np.dtype("q")`` and ``np.dtype("Q")`` are ``LongLongDType`` and
``ULongLongDType`` instances rather than ``Int64DType`` and ``UInt64DType``
instances, even though on this platform they describe the same layout. They
must still resolve to exactly one data type. See issue #3282.
"""
dtype = np.dtype(dtype_str)
assert isinstance(data_type_registry.match_dtype(dtype), expected_cls)
for zarr_format in (2, 3):
assert isinstance(parse_dtype(dtype, zarr_format=zarr_format), expected_cls)

@staticmethod
def test_unregistered_dtype(data_type_registry_fixture: DataTypeRegistry) -> None:
"""
Expand Down
Loading