Skip to content

Commit

Permalink
Merge pull request #496 from doronbehar/fix-np.bool_
Browse files Browse the repository at this point in the history
DeepHash: check numpy booleans like native booleans
  • Loading branch information
seperman authored Oct 25, 2024
2 parents cdc4b30 + cee3d41 commit 5d30b3a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion deepdiff/deephash.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import polars
except ImportError:
polars = False
try:
import numpy as np
booleanTypes = (bool, np.bool_)
except ImportError:
booleanTypes = bool

Check warning on line 31 in deepdiff/deephash.py

View check run for this annotation

Codecov / codecov/patch

deepdiff/deephash.py#L30-L31

Added lines #L30 - L31 were not covered by tests

logger = logging.getLogger(__name__)

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

if isinstance(obj, bool):
if isinstance(obj, booleanTypes):
obj = self._prep_bool(obj)
result = None
elif self.use_enum_value and isinstance(obj, Enum):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ def test_re(self):
a_hash = DeepHash(a)[a]
assert not( a_hash is unprocessed)

# https://github.com/seperman/deepdiff/issues/494
def test_numpy_bool(self):
a = {'b': np.array([True], dtype='bool')}
a_hash = DeepHash(a)[a]
assert not( a_hash is unprocessed)

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

Expand Down

0 comments on commit 5d30b3a

Please sign in to comment.