Skip to content

Commit 892657e

Browse files
committed
Switched to positional-only parameters
1 parent 4808799 commit 892657e

5 files changed

Lines changed: 28 additions & 28 deletions

File tree

pgvector/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
ndarray = None
1212

1313

14-
def is_ndarray(value: object) -> bool:
14+
def is_ndarray(value: object, /) -> bool:
1515
if (numpy := sys.modules.get('numpy')):
1616
return isinstance(value, numpy.ndarray)
1717
return False
1818

1919

20-
def is_sparse_array(value: object) -> bool:
20+
def is_sparse_array(value: object, /) -> bool:
2121
if (sparse := sys.modules.get('scipy.sparse')):
2222
return isinstance(value, (sparse.sparray, sparse.spmatrix))
2323
return False

pgvector/bit.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Bit:
1111
_length: int
1212
_data: bytes
1313

14-
def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int, ...], np.dtype[np.bool | np.uint8]]) -> None:
14+
def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int, ...], np.dtype[np.bool | np.uint8]], /) -> None:
1515
if isinstance(value, bytes):
1616
self._length = 8 * len(value)
1717
self._data = value
@@ -52,7 +52,7 @@ def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int, ...],
5252
def __repr__(self) -> str:
5353
return f'Bit({self.to_text()})'
5454

55-
def __eq__(self, other: object) -> bool:
55+
def __eq__(self, other: object, /) -> bool:
5656
if isinstance(other, self.__class__):
5757
return self._length == other._length and self._data == other._data
5858
return False
@@ -73,12 +73,12 @@ def to_binary(self) -> bytes:
7373
return pack('>i', self._length) + self._data
7474

7575
@classmethod
76-
def from_text(cls, value: str) -> Bit:
76+
def from_text(cls, value: str, /) -> Bit:
7777
# cast to ensure always uses str constructor
7878
return cls(str(value))
7979

8080
@classmethod
81-
def from_binary(cls, value: bytes | bytearray | memoryview) -> Bit:
81+
def from_binary(cls, value: bytes | bytearray | memoryview, /) -> Bit:
8282
length, = unpack_from('>i', value)
8383
data = memoryview(value)[4:].tobytes()
8484

pgvector/halfvec.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class HalfVector:
1313
_value: array[int] # uses uint16 since no float16
1414

15-
def __init__(self, value: list[float] | ndarray) -> None:
15+
def __init__(self, value: list[float] | ndarray, /) -> None:
1616
if isinstance(value, list):
1717
dim = len(value)
1818
try:
@@ -34,7 +34,7 @@ def __init__(self, value: list[float] | ndarray) -> None:
3434
def __repr__(self) -> str:
3535
return f'HalfVector({self.to_list()})'
3636

37-
def __eq__(self, other: object) -> bool:
37+
def __eq__(self, other: object, /) -> bool:
3838
if isinstance(other, self.__class__):
3939
return self._value == other._value
4040
return False
@@ -62,11 +62,11 @@ def to_binary(self) -> bytes:
6262
return struct.pack('>HH', len(value), 0) + memoryview(value)
6363

6464
@classmethod
65-
def from_text(cls, value: str) -> HalfVector:
65+
def from_text(cls, value: str, /) -> HalfVector:
6666
return cls(cls._from_text(value))
6767

6868
@classmethod
69-
def from_binary(cls, value: bytes | bytearray | memoryview) -> HalfVector:
69+
def from_binary(cls, value: bytes | bytearray | memoryview, /) -> HalfVector:
7070
dim, unused = struct.unpack_from('>HH', value)
7171
data = memoryview(value)[4:]
7272

@@ -86,11 +86,11 @@ def from_binary(cls, value: bytes | bytearray | memoryview) -> HalfVector:
8686
return vec
8787

8888
@classmethod
89-
def _from_text(cls, value: str) -> list[float]:
89+
def _from_text(cls, value: str, /) -> list[float]:
9090
return [float(v) for v in value[1:-1].split(',')]
9191

9292
@classmethod
93-
def _to_db(cls, value: list[float] | ndarray | HalfVector | None) -> str | None:
93+
def _to_db(cls, value: list[float] | ndarray | HalfVector | None, /) -> str | None:
9494
if value is None:
9595
return value
9696

@@ -104,7 +104,7 @@ def _to_db(cls, value: list[float] | ndarray | HalfVector | None) -> str | None:
104104
return value.to_text()
105105

106106
@classmethod
107-
def _from_db(cls, value: str | HalfVector | None) -> list[float] | None:
107+
def _from_db(cls, value: str | HalfVector | None, /) -> list[float] | None:
108108
if value is None:
109109
return value
110110

pgvector/sparsevec.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __repr__(self) -> str:
5252
elements = dict(zip(self._indices, self._values))
5353
return f'SparseVector({elements}, {self._dim})'
5454

55-
def __eq__(self, other: object) -> bool:
55+
def __eq__(self, other: object, /) -> bool:
5656
if isinstance(other, self.__class__):
5757
return self._dim == other._dim and self._indices == other._indices and self._values == other._values
5858
return False
@@ -102,7 +102,7 @@ def _from_dict(self, d: dict[int, float], dim: int) -> None:
102102
self._indices = [int(v[0]) for v in elements]
103103
self._values = [float(v[1]) for v in elements]
104104

105-
def _from_sparse(self, arr: sparray | spmatrix) -> None:
105+
def _from_sparse(self, arr: sparray | spmatrix, /) -> None:
106106
value: coo_array | coo_matrix = arr.tocoo(copy=False) # type: ignore
107107

108108
shape = cast(tuple[int], value.shape)
@@ -120,13 +120,13 @@ def _from_sparse(self, arr: sparray | spmatrix) -> None:
120120
self._indices = value.col.tolist()
121121
self._values = value.data.tolist()
122122

123-
def _from_dense(self, value: list[float] | ndarray) -> None:
123+
def _from_dense(self, value: list[float] | ndarray, /) -> None:
124124
self._dim = len(value)
125125
self._indices = [i for i, v in enumerate(value) if v != 0]
126126
self._values = [float(value[i]) for i in self._indices]
127127

128128
@classmethod
129-
def from_text(cls, value: str) -> SparseVector:
129+
def from_text(cls, value: str, /) -> SparseVector:
130130
elements, dim = value.split('/', 2)
131131
indices: list[int] = []
132132
values: list[float] = []
@@ -139,7 +139,7 @@ def from_text(cls, value: str) -> SparseVector:
139139
return cls._from_parts(int(dim), indices, values)
140140

141141
@classmethod
142-
def from_binary(cls, value: bytes | bytearray | memoryview) -> SparseVector:
142+
def from_binary(cls, value: bytes | bytearray | memoryview, /) -> SparseVector:
143143
dim, nnz, unused = unpack_from('>iii', value)
144144

145145
if len(value) != 12 + 8 * nnz:
@@ -153,15 +153,15 @@ def from_binary(cls, value: bytes | bytearray | memoryview) -> SparseVector:
153153
return cls._from_parts(dim, indices, values)
154154

155155
@classmethod
156-
def _from_parts(cls, dim: int, indices: list[int], values: list[float]) -> SparseVector:
156+
def _from_parts(cls, dim: int, indices: list[int], values: list[float], /) -> SparseVector:
157157
vec = cls.__new__(cls)
158158
vec._dim = dim
159159
vec._indices = indices
160160
vec._values = values
161161
return vec
162162

163163
@classmethod
164-
def _to_db(cls, value: list[float] | ndarray | sparray | spmatrix | SparseVector | None) -> str | None:
164+
def _to_db(cls, value: list[float] | ndarray | sparray | spmatrix | SparseVector | None, /) -> str | None:
165165
if value is None:
166166
return value
167167

@@ -171,7 +171,7 @@ def _to_db(cls, value: list[float] | ndarray | sparray | spmatrix | SparseVector
171171
return value.to_text()
172172

173173
@classmethod
174-
def _from_db(cls, value: str | SparseVector | None) -> SparseVector | None:
174+
def _from_db(cls, value: str | SparseVector | None, /) -> SparseVector | None:
175175
if value is None or isinstance(value, SparseVector):
176176
return value
177177

pgvector/vector.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Vector:
1313
_value: array[float]
1414

15-
def __init__(self, value: list[float] | ndarray) -> None:
15+
def __init__(self, value: list[float] | ndarray, /) -> None:
1616
if isinstance(value, list):
1717
try:
1818
self._value = array('f', value)
@@ -33,7 +33,7 @@ def __init__(self, value: list[float] | ndarray) -> None:
3333
def __repr__(self) -> str:
3434
return f'Vector({self.to_list()})'
3535

36-
def __eq__(self, other: object) -> bool:
36+
def __eq__(self, other: object, /) -> bool:
3737
if isinstance(other, self.__class__):
3838
return self._value == other._value
3939
return False
@@ -60,11 +60,11 @@ def to_binary(self) -> bytes:
6060
return struct.pack('>HH', len(value), 0) + memoryview(value)
6161

6262
@classmethod
63-
def from_text(cls, value: str) -> Vector:
63+
def from_text(cls, value: str, /) -> Vector:
6464
return cls(cls._from_text(value))
6565

6666
@classmethod
67-
def from_binary(cls, value: bytes | bytearray | memoryview) -> Vector:
67+
def from_binary(cls, value: bytes | bytearray | memoryview, /) -> Vector:
6868
dim, unused = struct.unpack_from('>HH', value)
6969
data = memoryview(value)[4:]
7070

@@ -84,11 +84,11 @@ def from_binary(cls, value: bytes | bytearray | memoryview) -> Vector:
8484
return vec
8585

8686
@classmethod
87-
def _from_text(cls, value: str) -> list[float]:
87+
def _from_text(cls, value: str, /) -> list[float]:
8888
return [float(v) for v in value[1:-1].split(',')]
8989

9090
@classmethod
91-
def _to_db(cls, value: list[float] | ndarray | Vector | None) -> str | None:
91+
def _to_db(cls, value: list[float] | ndarray | Vector | None, /) -> str | None:
9292
if value is None:
9393
return value
9494

@@ -102,7 +102,7 @@ def _to_db(cls, value: list[float] | ndarray | Vector | None) -> str | None:
102102
return value.to_text()
103103

104104
@classmethod
105-
def _from_db(cls, value: str | Vector | None) -> list[float] | None:
105+
def _from_db(cls, value: str | Vector | None, /) -> list[float] | None:
106106
if value is None:
107107
return value
108108

0 commit comments

Comments
 (0)