Skip to content

Commit 6c05256

Browse files
committed
issue/497 - add dtype __eq__ and __hash__
1 parent 511aeb8 commit 6c05256

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

python/infinicore/dtype.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
class dtype:
55
def __init__(self, data_type):
66
"""An internal method. Please do not use this directly."""
7-
87
self._underlying = data_type
98

109
def __repr__(self):
@@ -29,9 +28,31 @@ def __repr__(self):
2928
_infinicore.DataType.C128: "complex128",
3029
_infinicore.DataType.BF16: "bfloat16",
3130
}
32-
3331
return f"infinicore.{repr_map[self._underlying]}"
3432

33+
def __eq__(self, other):
34+
"""
35+
Compare two dtype objects for equality.
36+
37+
Args:
38+
other: The object to compare with
39+
40+
Returns:
41+
bool: True if both objects are dtype instances with the same underlying data type
42+
"""
43+
if not isinstance(other, dtype):
44+
return False
45+
return self._underlying == other._underlying
46+
47+
def __hash__(self):
48+
"""
49+
Return a hash value for the dtype object.
50+
51+
Returns:
52+
int: Hash value based on the underlying data type
53+
"""
54+
return hash(self._underlying)
55+
3556

3657
float32 = dtype(_infinicore.DataType.F32)
3758
float = float32

0 commit comments

Comments
 (0)