|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, Any |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +from zarr.core.array_spec import ArrayConfig, ArraySpec |
| 9 | +from zarr.core.buffer import BufferPrototype, default_buffer_prototype |
| 10 | +from zarr.core.buffer.cpu import NDBuffer |
| 11 | +from zarr.core.dtype import get_data_type_from_native_dtype |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + from collections.abc import Callable |
| 15 | + |
| 16 | + from zarr.core.common import MemoryOrder |
| 17 | + |
| 18 | + |
| 19 | +def _make_spec( |
| 20 | + *, |
| 21 | + shape: tuple[int, ...] = (4, 4), |
| 22 | + native_dtype: Any = "int16", |
| 23 | + fill_value: Any = 0, |
| 24 | + order: MemoryOrder = "C", |
| 25 | + write_empty_chunks: bool = False, |
| 26 | + prototype: BufferPrototype | None = None, |
| 27 | +) -> ArraySpec: |
| 28 | + """Creates an ArraySpec with common defaults""" |
| 29 | + zdtype = get_data_type_from_native_dtype(np.dtype(native_dtype)) |
| 30 | + fill_value = zdtype.cast_scalar(fill_value) # mirrors ArrayV3Metadata's fill_value |
| 31 | + return ArraySpec( |
| 32 | + shape=shape, |
| 33 | + dtype=zdtype, |
| 34 | + fill_value=fill_value, |
| 35 | + config=ArrayConfig(order=order, write_empty_chunks=write_empty_chunks), |
| 36 | + prototype=prototype if prototype is not None else default_buffer_prototype(), |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +class _AltNDBuffer(NDBuffer): |
| 41 | + """A distinct NDBuffer subclass""" |
| 42 | + |
| 43 | + |
| 44 | +_ALT_PROTOTYPE = BufferPrototype( |
| 45 | + buffer=default_buffer_prototype().buffer, |
| 46 | + nd_buffer=_AltNDBuffer, |
| 47 | +) # a distinct BufferPrototype with a different nd_buffer subclass |
| 48 | + |
| 49 | + |
| 50 | +# Difficult / important cases: |
| 51 | +# issue #3054: np.void is unhashable when writeable |
| 52 | +# nan/NaT aren't self-equal yet must compare equal for a ArraySpec |
| 53 | +SPECS = [ |
| 54 | + pytest.param({"native_dtype": "int16", "fill_value": 7}, id="int16"), |
| 55 | + pytest.param({"native_dtype": "float64", "fill_value": 1.5}, id="float64"), |
| 56 | + pytest.param({"native_dtype": "float64", "fill_value": float("nan")}, id="float64-nan"), |
| 57 | + pytest.param({"native_dtype": "float64", "fill_value": -0.0}, id="float64-negzero"), |
| 58 | + pytest.param({"native_dtype": "complex128", "fill_value": 1 + 2j}, id="complex128"), |
| 59 | + pytest.param( |
| 60 | + {"native_dtype": "complex128", "fill_value": complex(-0.0, -0.0)}, |
| 61 | + id="complex128-negzero", |
| 62 | + ), |
| 63 | + pytest.param({"native_dtype": "bool", "fill_value": True}, id="bool"), |
| 64 | + pytest.param( |
| 65 | + {"native_dtype": "datetime64[s]", "fill_value": np.datetime64("2020-01-01")}, |
| 66 | + id="datetime64", |
| 67 | + ), |
| 68 | + pytest.param( |
| 69 | + {"native_dtype": "datetime64[s]", "fill_value": np.datetime64("NaT", "s")}, |
| 70 | + id="datetime64-NaT", |
| 71 | + ), |
| 72 | + pytest.param( |
| 73 | + {"native_dtype": [("a", "f8"), ("b", "i8")], "fill_value": (1.0, 2)}, |
| 74 | + id="structured-void", |
| 75 | + ), |
| 76 | + pytest.param({"native_dtype": "U5", "fill_value": "hello"}, id="fixed-string"), |
| 77 | + pytest.param({"shape": ()}, id="scalar-shape"), |
| 78 | + pytest.param({"shape": (0,)}, id="zero-size"), |
| 79 | + pytest.param({"order": "F"}, id="order-F"), |
| 80 | +] |
| 81 | + |
| 82 | + |
| 83 | +# Mutations: each mutate kwargs to an unequal version |
| 84 | +def _grow_shape(kw: dict[str, Any]) -> dict[str, Any]: |
| 85 | + return {"shape": (*kw.get("shape", (4, 4)), 1)} |
| 86 | + |
| 87 | + |
| 88 | +def _flip_order(kw: dict[str, Any]) -> dict[str, Any]: |
| 89 | + return {"order": "F" if kw.get("order", "C") == "C" else "C"} |
| 90 | + |
| 91 | + |
| 92 | +def _swap_prototype(_kw: dict[str, Any]) -> dict[str, Any]: |
| 93 | + return {"prototype": _ALT_PROTOTYPE} |
| 94 | + |
| 95 | + |
| 96 | +MUTATIONS = [ |
| 97 | + pytest.param(_grow_shape, id="shape"), |
| 98 | + pytest.param(_flip_order, id="order"), |
| 99 | + pytest.param(_swap_prototype, id="prototype"), |
| 100 | +] |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("kwargs", SPECS) |
| 104 | +def test_hashable(kwargs: dict[str, Any]) -> None: |
| 105 | + """Every ArraySpec is hashable, including structured (np.void) fill values.""" |
| 106 | + assert isinstance(hash(_make_spec(**kwargs)), int) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parametrize("kwargs", SPECS) |
| 110 | +def test_equal_specs_hash_equal(kwargs: dict[str, Any]) -> None: |
| 111 | + """Independently built specs with identical fields are equal and hash equal.""" |
| 112 | + a = _make_spec(**kwargs) |
| 113 | + b = _make_spec(**kwargs) |
| 114 | + assert a == b |
| 115 | + assert hash(a) == hash(b) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize("kwargs", SPECS) |
| 119 | +@pytest.mark.parametrize("mutate", MUTATIONS) |
| 120 | +def test_distinct_specs_unequal( |
| 121 | + mutate: Callable[[dict[str, Any]], dict[str, Any]], |
| 122 | + kwargs: dict[str, Any], |
| 123 | +) -> None: |
| 124 | + """Changing one dtype-independent field makes a spec unequal to its base.""" |
| 125 | + base = _make_spec(**kwargs) |
| 126 | + variant = _make_spec(**{**kwargs, **mutate(kwargs)}) |
| 127 | + assert base != variant |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize( |
| 131 | + ("base", "variant"), |
| 132 | + [ |
| 133 | + pytest.param({"fill_value": 0}, {"fill_value": 1}, id="fill_value"), |
| 134 | + pytest.param({"native_dtype": "int16"}, {"native_dtype": "int32"}, id="dtype"), |
| 135 | + pytest.param( |
| 136 | + {"native_dtype": "float32", "fill_value": 1.0}, |
| 137 | + {"native_dtype": "float64", "fill_value": 1.0}, |
| 138 | + id="dtype-float-promote", |
| 139 | + ), |
| 140 | + ], |
| 141 | +) |
| 142 | +def test_dtype_and_fill_value_matter(base: dict[str, Any], variant: dict[str, Any]) -> None: |
| 143 | + """dtype and fill_value participate in equality; they can't join the cross |
| 144 | + product because fill_value is coupled to dtype.""" |
| 145 | + assert _make_spec(**base) != _make_spec(**variant) |
| 146 | + |
| 147 | + |
| 148 | +@pytest.mark.parametrize( |
| 149 | + ("native_dtype", "neg_fill", "pos_fill"), |
| 150 | + [ |
| 151 | + pytest.param("float16", -0.0, 0.0, id="float16"), |
| 152 | + pytest.param("float32", -0.0, 0.0, id="float32"), |
| 153 | + pytest.param("float64", -0.0, 0.0, id="float64"), |
| 154 | + pytest.param("complex128", complex(-0.0, -0.0), 0j, id="complex128-both"), |
| 155 | + pytest.param("complex128", complex(0.0, -0.0), 0j, id="complex128-imag"), |
| 156 | + pytest.param("complex128", complex(-0.0, 0.0), 0j, id="complex128-real"), |
| 157 | + pytest.param([("a", "f8")], (-0.0,), (0.0,), id="structured"), |
| 158 | + ], |
| 159 | +) |
| 160 | +def test_signed_zero_fills_are_distinct(native_dtype: Any, neg_fill: Any, pos_fill: Any) -> None: |
| 161 | + """A -0.0 fill writes different bytes than +0.0, so the specs are not equal.""" |
| 162 | + neg = _make_spec(native_dtype=native_dtype, fill_value=neg_fill) |
| 163 | + pos = _make_spec(native_dtype=native_dtype, fill_value=pos_fill) |
| 164 | + assert neg != pos |
| 165 | + |
| 166 | + |
| 167 | +@pytest.mark.parametrize( |
| 168 | + ("obj"), |
| 169 | + [ |
| 170 | + pytest.param(None, id="None"), |
| 171 | + pytest.param(42, id="int"), |
| 172 | + pytest.param("hello", id="str"), |
| 173 | + pytest.param([1, 2, 3], id="list"), |
| 174 | + pytest.param({"a": 1}, id="dict"), |
| 175 | + ], |
| 176 | +) |
| 177 | +def test_unequal_with_invalid_type(obj: Any) -> None: |
| 178 | + assert (_make_spec() == obj) is False |
| 179 | + assert _make_spec() != obj |
0 commit comments