Skip to content

Commit 12ffb43

Browse files
committed
Fixed CI
1 parent 1868e6d commit 12ffb43

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

pgvector/psycopg/vector.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
from psycopg.adapt import Loader, Dumper
44
from psycopg.pq import Format
55
from psycopg.types import TypeInfo
6-
from typing import Any, TypeAlias, TYPE_CHECKING
6+
from typing import Any, TypeAlias
77
from .. import Vector
88

99
Buffer: TypeAlias = bytes | bytearray | memoryview
1010

11-
if TYPE_CHECKING:
11+
try:
1212
import numpy as np
13+
NUMPY_AVAILABLE = True
14+
except ImportError:
15+
NUMPY_AVAILABLE = False
1316

1417

1518
class VectorDumper(Dumper):
@@ -58,9 +61,11 @@ def register_vector_info(context: BaseConnection[Any], info: TypeInfo | None) ->
5861
binary_dumper = type('', (VectorBinaryDumper,), {'oid': info.oid})
5962

6063
adapters = context.adapters
61-
adapters.register_dumper('numpy.ndarray', text_dumper)
62-
adapters.register_dumper('numpy.ndarray', binary_dumper)
6364
adapters.register_dumper(Vector, text_dumper)
6465
adapters.register_dumper(Vector, binary_dumper)
6566
adapters.register_loader(info.oid, VectorLoader)
6667
adapters.register_loader(info.oid, VectorBinaryLoader)
68+
69+
if NUMPY_AVAILABLE:
70+
adapters.register_dumper(np.ndarray, text_dumper)
71+
adapters.register_dumper(np.ndarray, binary_dumper)

pgvector/psycopg2/vector.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from psycopg2.extensions import adapt, connection, cursor, new_array_type, new_type, register_adapter, register_type
2-
from typing import TYPE_CHECKING
32
from .. import Vector
43

5-
if TYPE_CHECKING:
4+
try:
65
import numpy as np
6+
NUMPY_AVAILABLE = True
7+
except ImportError:
8+
NUMPY_AVAILABLE = False
79

810

911
class VectorAdapter:
@@ -32,8 +34,5 @@ def register_vector_info(oid: int, array_oid: int | None, scope: connection | cu
3234

3335
register_adapter(Vector, VectorAdapter)
3436

35-
try:
36-
import numpy as np
37+
if NUMPY_AVAILABLE:
3738
register_adapter(np.ndarray, VectorAdapter)
38-
except ImportError:
39-
pass

0 commit comments

Comments
 (0)