Skip to content

Commit 901eeff

Browse files
authored
Merge pull request #537 from jcipar/jcipar/534-np-datetime64
Fix recursion depth limit when hashing numpy.datetime64
2 parents bd6b60b + ed57957 commit 901eeff

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

deepdiff/helper.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __repr__(self):
5858
np_complex128 = np_type # pragma: no cover.
5959
np_cdouble = np_type # pragma: no cover.
6060
np_complexfloating = np_type # pragma: no cover.
61+
np_datetime64 = np_type # pragma: no cover.
6162
else:
6263
np_array_factory = np.array
6364
np_ndarray = np.ndarray
@@ -80,6 +81,7 @@ def __repr__(self):
8081
np_complex128 = np.complex128
8182
np_cdouble = np.cdouble # np.complex_ is an alias for np.cdouble and is being removed by NumPy 2.0
8283
np_complexfloating = np.complexfloating
84+
np_datetime64 = np.datetime64
8385

8486
numpy_numbers = (
8587
np_int8, np_int16, np_int32, np_int64, np_uint8,
@@ -93,6 +95,7 @@ def __repr__(self):
9395

9496
numpy_dtypes = set(numpy_numbers)
9597
numpy_dtypes.add(np_bool_) # type: ignore
98+
numpy_dtypes.add(np_datetime64) # type: ignore
9699

97100
numpy_dtype_str_to_type = {
98101
item.__name__: item for item in numpy_dtypes
@@ -184,10 +187,10 @@ def get_semvar_as_integer(version):
184187
bytes_type = bytes
185188
only_complex_number = (complex,) + numpy_complex_numbers
186189
only_numbers = (int, float, complex, Decimal) + numpy_numbers
187-
datetimes = (datetime.datetime, datetime.date, datetime.timedelta, datetime.time)
190+
datetimes = (datetime.datetime, datetime.date, datetime.timedelta, datetime.time, np_datetime64)
188191
ipranges = (ipaddress.IPv4Interface, ipaddress.IPv6Interface, ipaddress.IPv4Network, ipaddress.IPv6Network)
189192
uuids = (uuid.UUID, )
190-
times = (datetime.datetime, datetime.time)
193+
times = (datetime.datetime, datetime.time,np_datetime64)
191194
numbers: Tuple = only_numbers + datetimes
192195
booleans = (bool, np_bool_)
193196

tests/test_diff_numpy.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,25 @@
143143
}
144144
},
145145
},
146+
'numpy_datetime_equal': {
147+
't1': np.datetime64('2023-07-05T10:11:12'),
148+
't2': np.datetime64('2023-07-05T10:11:12'),
149+
'deepdiff_kwargs': {},
150+
'expected_result': {},
151+
},
152+
'numpy_datetime_unequal': {
153+
't1': np.datetime64('2023-07-05T10:11:12'),
154+
't2': np.datetime64('2024-07-05T10:11:12'),
155+
'deepdiff_kwargs': {},
156+
'expected_result': {
157+
'values_changed': {
158+
'root': {
159+
'new_value': np.datetime64('2024-07-05T10:11:12'),
160+
'old_value': np.datetime64('2023-07-05T10:11:12'),
161+
}
162+
},
163+
},
164+
},
146165
}
147166

148167

tests/test_hash.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ def test_numpy_bool(self):
196196
a_hash = DeepHash(a)[a]
197197
assert not( a_hash is unprocessed)
198198

199+
def test_numpy_datetime64(self):
200+
now_dt = datetime.datetime.now()
201+
now = np.datetime64(now_dt)
202+
later = np.datetime64(now_dt + datetime.timedelta(seconds=10))
203+
a = b = now
204+
a_hash = DeepHash(a)
205+
b_hash = DeepHash(b)
206+
assert a_hash[a] == b_hash[b]
207+
208+
later_hash = DeepHash(later)
209+
assert a_hash[a] != later_hash[later]
210+
211+
199212
class TestDeepHashPrep:
200213
"""DeepHashPrep Tests covering object serialization."""
201214

0 commit comments

Comments
 (0)