Skip to content

Commit e4f4218

Browse files
Updated hash key generation to handling missing encoding dtype. Also added tests to validate fix and updated the development change notes.
1 parent 0bbb470 commit e4f4218

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

docs/releases/development.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
Next release (in development)
33
=============================
44

5-
* ...
5+
* Fix datasets hash_key generation when geometry encoding
6+
is missing a dtype (:pr:`166`).

src/emsarray/conventions/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1973,7 +1973,7 @@ def hash_geometry(self, hash: "hashlib._Hash") -> None:
19731973
# Include the dtype of the data array.
19741974
# A float array and an int array mean very different things,
19751975
# but could have identical byte patterns.
1976-
hash_string(hash, data_array.encoding['dtype'].name)
1976+
hash_string(hash, data_array.encoding.get('dtype', data_array.values.dtype).name)
19771977

19781978
# Include the size and shape of the data.
19791979
# 1D coordinate arrays are very different to 2D coordinate arrays,

tests/operations/test_cache.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,25 @@ def test_cache_key_cfgrid1d_sha1(datasets: pathlib.Path):
200200
assert result_cache_key_cf is not None
201201

202202
assert result_cache_key_cf == cache_key_hash_cf1d_sha1
203+
204+
205+
def test_cache_key_with_missing_data_array_encoding_type(datasets: pathlib.Path):
206+
dataset_ugrid = emsarray.open_dataset(datasets / 'ugrid_mesh2d.nc')
207+
208+
data_array = dataset_ugrid.ems.topology.face_node_connectivity
209+
data_array_dtype_dropped = data_array.copy()
210+
data_array_dtype_dropped.encoding.pop('dtype', None)
211+
212+
with_dtype_hash = hashlib.sha1()
213+
without_dtype_hash = hashlib.sha1()
214+
215+
dataset_ugrid.ems.hash_geometry(with_dtype_hash)
216+
dataset_ugrid['mesh_face_node'] = data_array_dtype_dropped
217+
dataset_ugrid.ems.hash_geometry(without_dtype_hash)
218+
219+
with_dtype_digest = with_dtype_hash.hexdigest()
220+
without_dtype_digest = without_dtype_hash.hexdigest()
221+
222+
assert with_dtype_digest != without_dtype_digest
223+
224+
assert data_array_dtype_dropped.equals(data_array)

0 commit comments

Comments
 (0)