Skip to content

Commit

Permalink
Improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 26, 2024
1 parent e005b45 commit 12370bb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pgvector/utils/sparsevec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class SparseVector:
def __init__(self, value, dimensions=NO_DEFAULT, /):
if value.__class__.__module__.startswith('scipy.sparse.'):
if dimensions is not NO_DEFAULT:
raise ValueError('dimensions not allowed')
raise ValueError('extra argument')

self._from_sparse(value)
elif isinstance(value, dict):
if dimensions is NO_DEFAULT:
raise ValueError('dimensions required')
raise ValueError('missing dimensions')

self._from_dict(value, dimensions)
else:
if dimensions is not NO_DEFAULT:
raise ValueError('dimensions not allowed')
raise ValueError('extra argument')

self._from_dense(value)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_sparse_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_list(self):
def test_list_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector([1, 0, 2, 0, 3, 0], 6)
assert str(error.value) == 'dimensions not allowed'
assert str(error.value) == 'extra argument'

def test_ndarray(self):
vec = SparseVector(np.array([1, 0, 2, 0, 3, 0]))
Expand All @@ -29,7 +29,7 @@ def test_dict(self):
def test_dict_no_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector({0: 1, 2: 2, 4: 3})
assert str(error.value) == 'dimensions required'
assert str(error.value) == 'missing dimensions'

def test_coo_array(self):
arr = coo_array(np.array([1, 0, 2, 0, 3, 0]))
Expand All @@ -40,7 +40,7 @@ def test_coo_array(self):
def test_coo_array_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector(coo_array(np.array([1, 0, 2, 0, 3, 0])), 6)
assert str(error.value) == 'dimensions not allowed'
assert str(error.value) == 'extra argument'

def test_dok_array(self):
arr = coo_array(np.array([1, 0, 2, 0, 3, 0])).todok()
Expand Down

0 comments on commit 12370bb

Please sign in to comment.