Skip to content

Fix recursion depth limit when hashing numpy.datetime64 #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions deepdiff/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __repr__(self):
np_complex128 = np_type # pragma: no cover.
np_cdouble = np_type # pragma: no cover.
np_complexfloating = np_type # pragma: no cover.
np_datetime64 = np_type # pragma: no cover.
else:
np_array_factory = np.array
np_ndarray = np.ndarray
Expand All @@ -80,6 +81,7 @@ def __repr__(self):
np_complex128 = np.complex128
np_cdouble = np.cdouble # np.complex_ is an alias for np.cdouble and is being removed by NumPy 2.0
np_complexfloating = np.complexfloating
np_datetime64 = np.datetime64

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

numpy_dtypes = set(numpy_numbers)
numpy_dtypes.add(np_bool_) # type: ignore
numpy_dtypes.add(np_datetime64) # type: ignore

numpy_dtype_str_to_type = {
item.__name__: item for item in numpy_dtypes
Expand Down Expand Up @@ -184,10 +187,10 @@ def get_semvar_as_integer(version):
bytes_type = bytes
only_complex_number = (complex,) + numpy_complex_numbers
only_numbers = (int, float, complex, Decimal) + numpy_numbers
datetimes = (datetime.datetime, datetime.date, datetime.timedelta, datetime.time)
datetimes = (datetime.datetime, datetime.date, datetime.timedelta, datetime.time, np_datetime64)
ipranges = (ipaddress.IPv4Interface, ipaddress.IPv6Interface, ipaddress.IPv4Network, ipaddress.IPv6Network)
uuids = (uuid.UUID, )
times = (datetime.datetime, datetime.time)
times = (datetime.datetime, datetime.time,np_datetime64)
numbers: Tuple = only_numbers + datetimes
booleans = (bool, np_bool_)

Expand Down
19 changes: 19 additions & 0 deletions tests/test_diff_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@
}
},
},
'numpy_datetime_equal': {
't1': np.datetime64('2023-07-05T10:11:12'),
't2': np.datetime64('2023-07-05T10:11:12'),
'deepdiff_kwargs': {},
'expected_result': {},
},
'numpy_datetime_unequal': {
't1': np.datetime64('2023-07-05T10:11:12'),
't2': np.datetime64('2024-07-05T10:11:12'),
'deepdiff_kwargs': {},
'expected_result': {
'values_changed': {
'root': {
'new_value': np.datetime64('2024-07-05T10:11:12'),
'old_value': np.datetime64('2023-07-05T10:11:12'),
}
},
},
},
}


Expand Down
13 changes: 13 additions & 0 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ def test_numpy_bool(self):
a_hash = DeepHash(a)[a]
assert not( a_hash is unprocessed)

def test_numpy_datetime64(self):
now_dt = datetime.datetime.now()
now = np.datetime64(now_dt)
later = np.datetime64(now_dt + datetime.timedelta(seconds=10))
a = b = now
a_hash = DeepHash(a)
b_hash = DeepHash(b)
assert a_hash[a] == b_hash[b]

later_hash = DeepHash(later)
assert a_hash[a] != later_hash[later]


class TestDeepHashPrep:
"""DeepHashPrep Tests covering object serialization."""

Expand Down