Skip to content
Merged
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/2801.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure utf8 compliant strings are used to construct numpy arrays in property-based tests
22 changes: 21 additions & 1 deletion src/zarr/testing/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ def v2_dtypes() -> st.SearchStrategy[np.dtype]:
)


def safe_unicode_for_dtype(dtype: np.dtype[np.str_]) -> st.SearchStrategy[str]:
"""Generate UTF-8-safe text constrained to max_len of dtype."""
# account for utf-32 encoding (i.e. 4 bytes/character)
max_len = max(1, dtype.itemsize // 4)

return st.text(
alphabet=st.characters(
blacklist_categories=["Cs"], # Avoid *technically allowed* surrogates
min_codepoint=32,
),
min_size=1,
max_size=max_len,
)


# From https://zarr-specs.readthedocs.io/en/latest/v3/core/v3.0.html#node-names
# 1. must not be the empty string ("")
# 2. must not include the character "/"
Expand Down Expand Up @@ -86,7 +101,12 @@ def numpy_arrays(
Generate numpy arrays that can be saved in the provided Zarr format.
"""
zarr_format = draw(zarr_formats)
return draw(npst.arrays(dtype=v3_dtypes() if zarr_format == 3 else v2_dtypes(), shape=shapes))
dtype = draw(v3_dtypes() if zarr_format == 3 else v2_dtypes())
if np.issubdtype(dtype, np.str_):
safe_unicode_strings = safe_unicode_for_dtype(dtype)
return draw(npst.arrays(dtype=dtype, shape=shapes, elements=safe_unicode_strings))

return draw(npst.arrays(dtype=dtype, shape=shapes))


@st.composite # type: ignore[misc]
Expand Down