Skip to content

Commit 5d30b3a

Browse files
authored
Merge pull request #496 from doronbehar/fix-np.bool_
DeepHash: check numpy booleans like native booleans
2 parents cdc4b30 + cee3d41 commit 5d30b3a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

deepdiff/deephash.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
import polars
2525
except ImportError:
2626
polars = False
27+
try:
28+
import numpy as np
29+
booleanTypes = (bool, np.bool_)
30+
except ImportError:
31+
booleanTypes = bool
2732

2833
logger = logging.getLogger(__name__)
2934

@@ -492,7 +497,7 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
492497
"""The main hash method"""
493498
counts = 1
494499

495-
if isinstance(obj, bool):
500+
if isinstance(obj, booleanTypes):
496501
obj = self._prep_bool(obj)
497502
result = None
498503
elif self.use_enum_value and isinstance(obj, Enum):

tests/test_hash.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ def test_re(self):
187187
a_hash = DeepHash(a)[a]
188188
assert not( a_hash is unprocessed)
189189

190+
# https://github.com/seperman/deepdiff/issues/494
191+
def test_numpy_bool(self):
192+
a = {'b': np.array([True], dtype='bool')}
193+
a_hash = DeepHash(a)[a]
194+
assert not( a_hash is unprocessed)
195+
190196
class TestDeepHashPrep:
191197
"""DeepHashPrep Tests covering object serialization."""
192198

0 commit comments

Comments
 (0)