Skip to content

Commit 7fdf69d

Browse files
committed
Updated __eq__ methods to return NotImplemented
1 parent 1e57b6f commit 7fdf69d

8 files changed

Lines changed: 16 additions & 12 deletions

File tree

pgvector/bit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def __repr__(self) -> str:
5757
return f'Bit({self.to_text()})'
5858

5959
def __eq__(self, other: object, /) -> bool:
60-
if isinstance(other, self.__class__):
61-
return self._length == other._length and self._data == other._data
62-
return False
60+
if not isinstance(other, self.__class__):
61+
return NotImplemented
62+
return self._length == other._length and self._data == other._data
6363

6464
def to_list(self) -> list[bool]:
6565
# TODO improve

pgvector/halfvec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def __repr__(self) -> str:
3535
return f'HalfVector({self.to_list()})'
3636

3737
def __eq__(self, other: object, /) -> bool:
38-
if isinstance(other, self.__class__):
39-
return self._value == other._value
40-
return False
38+
if not isinstance(other, self.__class__):
39+
return NotImplemented
40+
return self._value == other._value
4141

4242
def dimensions(self) -> int:
4343
return len(self._value)

pgvector/sparsevec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def __repr__(self) -> str:
5353
return f'SparseVector({elements}, {self._dim})'
5454

5555
def __eq__(self, other: object, /) -> bool:
56-
if isinstance(other, self.__class__):
57-
return self._dim == other._dim and self._indices == other._indices and self._values == other._values
58-
return False
56+
if not isinstance(other, self.__class__):
57+
return NotImplemented
58+
return self._dim == other._dim and self._indices == other._indices and self._values == other._values
5959

6060
def dimensions(self) -> int:
6161
return self._dim

pgvector/vector.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def __repr__(self) -> str:
3434
return f'Vector({self.to_list()})'
3535

3636
def __eq__(self, other: object, /) -> bool:
37-
if isinstance(other, self.__class__):
38-
return self._value == other._value
39-
return False
37+
if not isinstance(other, self.__class__):
38+
return NotImplemented
39+
return self._value == other._value
4040

4141
def dimensions(self) -> int:
4242
return len(self._value)

tests/test_bit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def test_repr(self) -> None:
8585
def test_equality(self) -> None:
8686
assert Bit([True, False, True]) == Bit([True, False, True])
8787
assert Bit([True, False, True]) != Bit([True, False, False])
88+
assert Bit([True, False, True]) != 1
8889

8990
def test_from_text(self) -> None:
9091
vec = Bit.from_text('101')

tests/test_half_vector.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def test_repr(self) -> None:
5454
def test_equality(self) -> None:
5555
assert HalfVector([1, 2, 3]) == HalfVector([1, 2, 3])
5656
assert HalfVector([1, 2, 3]) != HalfVector([1, 2, 4])
57+
assert HalfVector([1, 2, 3]) != 1
5758

5859
def test_dimensions(self) -> None:
5960
assert HalfVector([1, 2, 3]).dimensions() == 3

tests/test_sparse_vector.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def test_equality(self) -> None:
112112
assert SparseVector([1, 0, 2, 0, 3, 0]) != SparseVector([1, 0, 2, 0, 3, 1])
113113
assert SparseVector([1, 0, 2, 0, 3, 0]) == SparseVector({2: 2, 4: 3, 0: 1, 3: 0}, 6)
114114
assert SparseVector({}, 1) != SparseVector({}, 2)
115+
assert SparseVector([1, 0, 2, 0, 3, 0]) != 1
115116

116117
def test_dimensions(self) -> None:
117118
assert SparseVector([1, 0, 2, 0, 3, 0]).dimensions() == 6

tests/test_vector.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def test_repr(self) -> None:
5454
def test_equality(self) -> None:
5555
assert Vector([1, 2, 3]) == Vector([1, 2, 3])
5656
assert Vector([1, 2, 3]) != Vector([1, 2, 4])
57+
assert Vector([1, 2, 3]) != 1
5758

5859
def test_dimensions(self) -> None:
5960
assert Vector([1, 2, 3]).dimensions() == 3

0 commit comments

Comments
 (0)