Repository: qutech/qupulse (language: python)

Issue:
Cleanup hashing of numpy arrays
`qupulse.utils.types.HashableNumpyArray` is inconsistent as it does not overwrite `__eq__` properly


Discussion / hints:
Currently only used in CosntantParameter. Will be deprecated.

Fix patch:
diff --git a/changes.d/408.removal b/changes.d/408.removal
new file mode 100644
index 00000000..6b0e4392
--- /dev/null
+++ b/changes.d/408.removal
@@ -0,0 +1,1 @@
+Deprecate HashableNumpyArray due to its inconsistency.
diff --git a/qupulse/utils/types.py b/qupulse/utils/types.py
index 23603b39..6c31fa06 100644
--- a/qupulse/utils/types.py
+++ b/qupulse/utils/types.py
@@ -364,10 +364,16 @@ def __init__(cls, name, bases, dct):
 class HashableNumpyArray(numpy.ndarray):
     """Make numpy arrays hashable.
 
+    Deprecated since 0.6. This is a bad idea.
+
     Example usage:
     my_array = np.zeros([1, 2, 3, 4])
     hashable = my_array.view(HashableNumpyArray)
     """
+    def __array_finalize__(self, obj):
+        warnings.warn("HashableNumpyArray is deprecated since qupulse 0.6 and will be removed in the next release.",
+                      category=DeprecationWarning, stacklevel=2)
+
     def __hash__(self):
         return hash(self.tobytes())
 


Test patch:
diff --git a/tests/utils/types_tests.py b/tests/utils/types_tests.py
index 7f954b30..e2271d2f 100644
--- a/tests/utils/types_tests.py
+++ b/tests/utils/types_tests.py
@@ -10,11 +10,12 @@
 class HashableNumpyArrayTest(unittest.TestCase):
     def test_hash(self):
 
-        a = np.array([1, 2, 3]).view(HashableNumpyArray)
+        with self.assertWarns(DeprecationWarning):
+            a = np.array([1, 2, 3]).view(HashableNumpyArray)
 
-        b = np.array([3, 4, 5]).view(HashableNumpyArray)
+            b = np.array([3, 4, 5]).view(HashableNumpyArray)
 
-        c = np.array([1, 2, 3]).view(HashableNumpyArray)
+            c = np.array([1, 2, 3]).view(HashableNumpyArray)
 
         self.assertNotEqual(hash(a), hash(b))
         self.assertEqual(hash(a), hash(c))