Skip to content

Commit

Permalink
Fix issue 480
Browse files Browse the repository at this point in the history
Deprecate HashableNumpyArray.
  • Loading branch information
terrorfisch committed Jul 8, 2021
2 parents 7cd11ee + d703124 commit 56e8498
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions changes.d/408.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate HashableNumpyArray due to its inconsistency.
6 changes: 6 additions & 0 deletions qupulse/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
7 changes: 4 additions & 3 deletions tests/utils/types_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 56e8498

Please sign in to comment.