Skip to content

Commit 643f8d1

Browse files
committed
Added ndarray type alias
1 parent c223ef4 commit 643f8d1

6 files changed

Lines changed: 22 additions & 13 deletions

File tree

pgvector/_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import sys
2+
from typing import TYPE_CHECKING, TypeAlias
3+
4+
if TYPE_CHECKING:
5+
import numpy as np
6+
7+
ndarray: TypeAlias = np.ndarray[tuple[int, ...], np.dtype[np.floating]]
28

39

410
def is_ndarray(value: object) -> bool:

pgvector/halfvec.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
if TYPE_CHECKING:
99
import numpy as np
10+
from ._utils import ndarray
1011

1112

1213
class HalfVector:
13-
def __init__(self, value: list[float] | np.ndarray[tuple[int, ...], np.dtype[np.floating]]) -> None:
14+
def __init__(self, value: list[float] | ndarray) -> None:
1415
if isinstance(value, list):
1516
dim = len(value)
1617
try:
@@ -88,7 +89,7 @@ def _from_text(cls, value: str) -> list[float]:
8889
return [float(v) for v in value[1:-1].split(',')]
8990

9091
@classmethod
91-
def _to_db(cls, value: list[float] | np.ndarray[tuple[int], np.dtype[np.floating]] | HalfVector | None) -> str | None:
92+
def _to_db(cls, value: list[float] | ndarray | HalfVector | None) -> str | None:
9293
if value is None:
9394
return value
9495

pgvector/psycopg/vector.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
Buffer: TypeAlias = bytes | bytearray | memoryview
1111

1212
if TYPE_CHECKING:
13-
import numpy as np
13+
from .._utils import ndarray
1414

1515

1616
class VectorDumper(Dumper):
1717
format = Format.TEXT
1818

19-
def dump(self, obj: Vector | np.ndarray[tuple[int, ...], np.dtype[np.floating]]) -> Buffer | None:
19+
def dump(self, obj: Vector | ndarray) -> Buffer | None:
2020
if not isinstance(obj, Vector):
2121
obj = Vector(obj)
2222
return obj.to_text().encode('utf8')
@@ -25,7 +25,7 @@ def dump(self, obj: Vector | np.ndarray[tuple[int, ...], np.dtype[np.floating]])
2525
class VectorBinaryDumper(VectorDumper):
2626
format = Format.BINARY
2727

28-
def dump(self, obj: Vector | np.ndarray[tuple[int, ...], np.dtype[np.floating]]) -> Buffer | None:
28+
def dump(self, obj: Vector | ndarray) -> Buffer | None:
2929
if not isinstance(obj, Vector):
3030
obj = Vector(obj)
3131
return obj.to_binary()

pgvector/psycopg2/vector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from .. import Vector
55

66
if TYPE_CHECKING:
7-
import numpy as np
7+
from .._utils import ndarray
88

99

1010
class VectorAdapter:
11-
def __init__(self, value: Vector | np.ndarray[tuple[int, ...], np.dtype[np.floating]]) -> None:
11+
def __init__(self, value: Vector | ndarray) -> None:
1212
if not isinstance(value, Vector):
1313
value = Vector(value)
1414
self._value = value

pgvector/sparsevec.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
if TYPE_CHECKING:
77
import numpy as np
88
from scipy.sparse import sparray, spmatrix, coo_array, coo_matrix
9+
from ._utils import ndarray
910

1011

1112
NO_DEFAULT = object()
@@ -17,10 +18,10 @@ def __init__(self, value: dict[int, float], dimensions: int, /) -> None:
1718
...
1819

1920
@overload
20-
def __init__(self, value: list[float] | np.ndarray[tuple[int, ...], np.dtype[np.floating]] | sparray | spmatrix, /) -> None:
21+
def __init__(self, value: list[float] | ndarray | sparray | spmatrix, /) -> None:
2122
...
2223

23-
def __init__(self, value: dict[int, float] | list[float] | np.ndarray[tuple[int, ...], np.dtype[np.floating]] | sparray | spmatrix, dimensions: int | Any = NO_DEFAULT, /) -> None:
24+
def __init__(self, value: dict[int, float] | list[float] | ndarray | sparray | spmatrix, dimensions: int | Any = NO_DEFAULT, /) -> None:
2425
if is_sparse_array(value):
2526
if dimensions is not NO_DEFAULT:
2627
raise ValueError('extra argument')
@@ -107,7 +108,7 @@ def _from_sparse(self, arr: sparray | spmatrix) -> None:
107108
self._indices = value.col.tolist()
108109
self._values = value.data.tolist()
109110

110-
def _from_dense(self, value: list[float] | np.ndarray[tuple[int], np.dtype[np.floating]]) -> None:
111+
def _from_dense(self, value: list[float] | ndarray) -> None:
111112
self._dim = len(value)
112113
self._indices = [i for i, v in enumerate(value) if v != 0]
113114
self._values = [float(value[i]) for i in self._indices]
@@ -148,7 +149,7 @@ def _from_parts(cls, dim: int, indices: list[int], values: list[float]) -> Spars
148149
return vec
149150

150151
@classmethod
151-
def _to_db(cls, value: list[float] | np.ndarray[tuple[int], np.dtype[np.floating]] | sparray | spmatrix | SparseVector | None) -> str | None:
152+
def _to_db(cls, value: list[float] | ndarray | sparray | spmatrix | SparseVector | None) -> str | None:
152153
if value is None:
153154
return value
154155

pgvector/vector.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
if TYPE_CHECKING:
99
import numpy as np
10+
from ._utils import ndarray
1011

1112

1213
class Vector:
13-
def __init__(self, value: list[float] | np.ndarray[tuple[int, ...], np.dtype[np.floating]]) -> None:
14+
def __init__(self, value: list[float] | ndarray) -> None:
1415
if isinstance(value, list):
1516
try:
1617
self._value = array.array('f', value)
@@ -86,7 +87,7 @@ def _from_text(cls, value: str) -> list[float]:
8687
return [float(v) for v in value[1:-1].split(',')]
8788

8889
@classmethod
89-
def _to_db(cls, value: list[float] | np.ndarray[tuple[int], np.dtype[np.floating]] | Vector | None) -> str | None:
90+
def _to_db(cls, value: list[float] | ndarray | Vector | None) -> str | None:
9091
if value is None:
9192
return value
9293

0 commit comments

Comments
 (0)