From 32df472cdb4c5c5eba6d8bfe8e6f1429649f6460 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Sun, 20 Oct 2024 02:55:11 +0300 Subject: [PATCH 1/2] DeepHash: check numpy booleans like native booleans Fixes #494 --- deepdiff/deephash.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/deepdiff/deephash.py b/deepdiff/deephash.py index 32fee9c..7c2e2b4 100644 --- a/deepdiff/deephash.py +++ b/deepdiff/deephash.py @@ -24,6 +24,11 @@ import polars except ImportError: polars = False +try: + import numpy as np + booleanTypes = (bool, np.bool_) +except ImportError: + booleanTypes = bool logger = logging.getLogger(__name__) @@ -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): From cee3d41868a9c973c48471f020f63380c271fad0 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Sun, 20 Oct 2024 20:05:48 +0300 Subject: [PATCH 2/2] TestDeepHash: test numpy booleans --- tests/test_hash.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_hash.py b/tests/test_hash.py index 5263757..22a86e2 100755 --- a/tests/test_hash.py +++ b/tests/test_hash.py @@ -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."""