Skip to content

Commit 24765f6

Browse files
committed
Added typing to attributes
1 parent 9409d64 commit 24765f6

4 files changed

Lines changed: 21 additions & 10 deletions

File tree

pgvector/bit.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99

1010
class Bit:
11+
_length: int
12+
_data: bytes
13+
1114
def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int, ...], np.dtype[np.bool | np.uint8]]) -> None:
1215
if isinstance(value, bytes):
1316
self._length = 8 * len(value)

pgvector/halfvec.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
import array
2+
from array import array
33
import struct
44
import sys
55
from typing import TYPE_CHECKING
@@ -10,11 +10,13 @@
1010

1111

1212
class HalfVector:
13+
_value: array[int]
14+
1315
def __init__(self, value: list[float] | ndarray) -> None:
1416
if isinstance(value, list):
1517
dim = len(value)
1618
try:
17-
self._value = array.array('H', struct.pack(f'{dim}e', *value))
19+
self._value = array('H', struct.pack(f'{dim}e', *value))
1820
except struct.error:
1921
raise ValueError('expected list[float]')
2022
elif is_ndarray(value):
@@ -23,7 +25,7 @@ def __init__(self, value: list[float] | ndarray) -> None:
2325
if value.ndim != 1:
2426
raise ValueError('expected ndim to be 1')
2527

26-
arr = array.array('H')
28+
arr = array('H')
2729
arr.frombytes(value.astype(np.float16, order='C', copy=False).data.cast('B'))
2830
self._value = arr
2931
else:
@@ -55,7 +57,7 @@ def to_binary(self) -> bytes:
5557
if sys.byteorder == 'big':
5658
value = self._value
5759
else:
58-
value = array.array('H', self._value)
60+
value = array('H', self._value)
5961
value.byteswap()
6062
return struct.pack('>HH', len(value), 0) + memoryview(value)
6163

@@ -74,7 +76,7 @@ def from_binary(cls, value: bytes | bytearray | memoryview) -> HalfVector:
7476
if unused != 0:
7577
raise ValueError('expected unused to be 0')
7678

77-
arr = array.array('H')
79+
arr = array('H')
7880
arr.frombytes(data)
7981
if sys.byteorder != 'big':
8082
arr.byteswap()

pgvector/sparsevec.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class Sentinel:
1616

1717

1818
class SparseVector:
19+
_dim: int
20+
_indices: list[int]
21+
_values: list[float]
22+
1923
@overload
2024
def __init__(self, value: dict[int, float], dimensions: int, /) -> None:
2125
...

pgvector/vector.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
import array
2+
from array import array
33
import struct
44
import sys
55
from typing import TYPE_CHECKING
@@ -10,10 +10,12 @@
1010

1111

1212
class Vector:
13+
_value: array[float]
14+
1315
def __init__(self, value: list[float] | ndarray) -> None:
1416
if isinstance(value, list):
1517
try:
16-
self._value = array.array('f', value)
18+
self._value = array('f', value)
1719
except TypeError:
1820
raise ValueError('expected list[float]')
1921
elif is_ndarray(value):
@@ -22,7 +24,7 @@ def __init__(self, value: list[float] | ndarray) -> None:
2224
if value.ndim != 1:
2325
raise ValueError('expected ndim to be 1')
2426

25-
arr = array.array('f')
27+
arr = array('f')
2628
arr.frombytes(value.astype(np.float32, order='C', copy=False).data.cast('B'))
2729
self._value = arr
2830
else:
@@ -53,7 +55,7 @@ def to_binary(self) -> bytes:
5355
if sys.byteorder == 'big':
5456
value = self._value
5557
else:
56-
value = array.array('f', self._value)
58+
value = array('f', self._value)
5759
value.byteswap()
5860
return struct.pack('>HH', len(value), 0) + memoryview(value)
5961

@@ -72,7 +74,7 @@ def from_binary(cls, value: bytes | bytearray | memoryview) -> Vector:
7274
if unused != 0:
7375
raise ValueError('expected unused to be 0')
7476

75-
arr = array.array('f')
77+
arr = array('f')
7678
arr.frombytes(data)
7779
if sys.byteorder != 'big':
7880
arr.byteswap()

0 commit comments

Comments
 (0)