Skip to content

Commit affa01c

Browse files
committed
override __eq__ method
1 parent 19675e4 commit affa01c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/zarr/abc/metadata.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from collections.abc import Sequence
4-
from typing import TYPE_CHECKING
4+
from typing import TYPE_CHECKING, Any
55

66
if TYPE_CHECKING:
77
from typing import Self
@@ -10,6 +10,8 @@
1010

1111
from dataclasses import dataclass, fields
1212

13+
import numpy as np
14+
1315
__all__ = ["Metadata"]
1416

1517

@@ -44,3 +46,23 @@ def from_dict(cls, data: dict[str, JSON]) -> Self:
4446
"""
4547

4648
return cls(**data)
49+
50+
def __eq__(self, other: Any) -> bool:
51+
"""Checks metadata are identical, including special treatment for NaN fill_values."""
52+
if not isinstance(other, type(self)):
53+
return False
54+
55+
metadata_dict1 = self.to_dict()
56+
metadata_dict2 = other.to_dict()
57+
58+
# fill_value is a special case because numpy NaNs cannot be compared using __eq__, see https://stackoverflow.com/a/10059796
59+
fill_value1 = metadata_dict1.pop("fill_value")
60+
fill_value2 = metadata_dict2.pop("fill_value")
61+
if np.isnan(fill_value1) and np.isnan(fill_value2):
62+
fill_values_equal = fill_value1.dtype == fill_value2.dtype
63+
else:
64+
fill_values_equal = fill_value1 == fill_value2
65+
66+
# everything else in ArrayV3Metadata is a string, Enum, or Dataclass
67+
return fill_values_equal and metadata_dict1 == metadata_dict2
68+

src/zarr/core/metadata/v3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class ArrayV3MetadataDict(TypedDict):
233233
attributes: dict[str, JSON]
234234

235235

236-
@dataclass(frozen=True, kw_only=True)
236+
@dataclass(frozen=True, kw_only=True, eq=False)
237237
class ArrayV3Metadata(Metadata):
238238
shape: ChunkCoords
239239
data_type: DataType

0 commit comments

Comments
 (0)