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
1 change: 1 addition & 0 deletions changes/3695.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise error when trying to encode :class:`numpy.dtypes.StringDType` with `na_object` set.
7 changes: 7 additions & 0 deletions src/zarr/core/dtype/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ def match_dtype(self, dtype: TBaseDType) -> ZDType[TBaseDType, TBaseScalar]:
"data type, see https://github.com/zarr-developers/zarr-python/issues/3117"
)
raise ValueError(msg)
if dtype.kind == "T" and hasattr(dtype, "na_object"):
msg = (
f"Zarr data type resolution from {dtype} failed. "
"Attempted to resolve a zarr data type from a `numpy.dtypes.StringDType` "
"with `na_object` set, which is not supported."
)
raise ValueError(msg)
Comment on lines +162 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you OK with moving this logic to the from_native_dtype method on the VariableLengthUTF8 class? It should be after we have checked that the input is numpy StringDType. I'm happy to implement this if that change is OK with you.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, go ahead! I just wanted to get this started!

matched: list[ZDType[TBaseDType, TBaseScalar]] = []
for val in self.contents.values():
with contextlib.suppress(DataTypeValidationError):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_dtype_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
get_data_type_from_json,
)
from zarr.core.dtype.common import unpack_dtype_json
from zarr.core.dtype.npy.string import _NUMPY_SUPPORTS_VLEN_STRING
from zarr.dtype import ( # type: ignore[attr-defined]
Bool,
FixedLengthUTF32,
Expand Down Expand Up @@ -74,6 +75,15 @@ def test_match_dtype(
data_type_registry_fixture.register(wrapper_cls._zarr_v3_name, wrapper_cls)
assert isinstance(data_type_registry_fixture.match_dtype(np.dtype(dtype_str)), wrapper_cls)

@pytest.mark.skipif(not _NUMPY_SUPPORTS_VLEN_STRING, reason="requires numpy with T dtype")
@staticmethod
def test_match_dtype_string_na_object_error(
data_type_registry_fixture: DataTypeRegistry,
) -> None:
dtype: np.dtype[Any] = np.dtypes.StringDType(na_object=None) # type: ignore[call-arg]
with pytest.raises(ValueError, match=r"Zarr data type resolution from StringDType.*failed"):
data_type_registry_fixture.match_dtype(dtype)

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