29
29
30
30
31
31
def deep_equal (a , b ):
32
- """Deep equality check w/ NaN e to handle array metadata serialization and deserialization behaviors """
32
+ """Deep equality check with handling of special cases for array metadata classes """
33
33
if isinstance (a , (complex , np .complexfloating )) and isinstance (
34
34
b , (complex , np .complexfloating )
35
35
):
36
- # Convert to Python float to force standard NaN handling.
37
36
a_real , a_imag = float (a .real ), float (a .imag )
38
37
b_real , b_imag = float (b .real ), float (b .imag )
39
- # If both parts are NaN, consider them equal.
40
38
if np .isnan (a_real ) and np .isnan (b_real ):
41
39
real_eq = True
42
40
else :
@@ -47,38 +45,31 @@ def deep_equal(a, b):
47
45
imag_eq = a_imag == b_imag
48
46
return real_eq and imag_eq
49
47
50
- # Handle floats (including numpy floating types) and treat NaNs as equal.
51
48
if isinstance (a , (float , np .floating )) and isinstance (b , (float , np .floating )):
52
49
if np .isnan (a ) and np .isnan (b ):
53
50
return True
54
51
return a == b
55
52
56
- # Handle numpy.datetime64 values, treating NaT as equal.
57
53
if isinstance (a , np .datetime64 ) and isinstance (b , np .datetime64 ):
58
54
if np .isnat (a ) and np .isnat (b ):
59
55
return True
60
56
return a == b
61
57
62
- # Handle numpy arrays.
63
58
if isinstance (a , np .ndarray ) and isinstance (b , np .ndarray ):
64
59
if a .shape != b .shape :
65
60
return False
66
- # Compare elementwise.
67
61
return all (deep_equal (x , y ) for x , y in zip (a .flat , b .flat , strict = False ))
68
62
69
- # Handle dictionaries.
70
63
if isinstance (a , dict ) and isinstance (b , dict ):
71
64
if set (a .keys ()) != set (b .keys ()):
72
65
return False
73
66
return all (deep_equal (a [k ], b [k ]) for k in a )
74
67
75
- # Handle lists and tuples.
76
68
if isinstance (a , (list , tuple )) and isinstance (b , (list , tuple )):
77
69
if len (a ) != len (b ):
78
70
return False
79
71
return all (deep_equal (x , y ) for x , y in zip (a , b , strict = False ))
80
72
81
- # Fallback to default equality.
82
73
return a == b
83
74
84
75
@@ -211,7 +202,6 @@ def test_meta_roundtrip(data: st.DataObject, zarr_format: int) -> None:
211
202
zarray_dict = json .loads (buffer_dict [ZARR_JSON ].to_bytes ().decode ())
212
203
metadata_roundtripped = ArrayV3Metadata .from_dict (zarray_dict )
213
204
214
- # Convert both metadata instances to dictionaries.
215
205
orig = dataclasses .asdict (metadata )
216
206
rt = dataclasses .asdict (metadata_roundtripped )
217
207
0 commit comments