psycopg-toolkit 0.3.0__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (19) hide show
  1. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/PKG-INFO +1 -1
  2. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/pyproject.toml +1 -5
  3. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/repositories/base.py +26 -39
  4. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/.gitignore +0 -0
  5. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/.vscode/settings.json +0 -0
  6. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/LICENSE +0 -0
  7. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/README.md +0 -0
  8. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/__init__.py +0 -0
  9. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/core/__init__.py +0 -0
  10. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/core/config.py +0 -0
  11. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/core/database.py +0 -0
  12. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/core/factory.py +0 -0
  13. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/core/transaction.py +0 -0
  14. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/exceptions.py +0 -0
  15. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/repositories/__init__.py +0 -0
  16. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/utils/__init__.py +0 -0
  17. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/utils/json_handler.py +0 -0
  18. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/utils/psychopg_helper.py +0 -0
  19. {psycopg_toolkit-0.3.0 → psycopg_toolkit-0.3.1}/src/psycopg_toolkit/utils/type_inspector.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: psycopg-toolkit
3
- Version: 0.3.0
3
+ Version: 0.3.1
4
4
  Summary: A Python PostgreSQL database utility with connection pooling
5
5
  Project-URL: Homepage, https://github.com/descoped/psycopg-toolkit
6
6
  Project-URL: Repository, https://github.com/descoped/psycopg-toolkit
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "psycopg-toolkit"
3
- version = "0.3.0"
3
+ version = "0.3.1"
4
4
  description = "A Python PostgreSQL database utility with connection pooling"
5
5
  authors = [
6
6
  { name = "Ove Ranheim", email = "oranheim@gmail.com" }
@@ -39,14 +39,10 @@ test = [
39
39
  "pytest-cov>=6.0.0",
40
40
  "testcontainers[postgres]>=4.12.0",
41
41
  "psutil>=6.1.0", # For performance benchmarks
42
- "pgvector>=0.4.1", # For pgvector integration tests
43
42
  ]
44
43
  dev = [
45
44
  "ruff>=0.8.0",
46
45
  ]
47
- vector = [
48
- "pgvector>=0.4.1",
49
- ]
50
46
 
51
47
  [build-system]
52
48
  requires = ["hatchling>=1.26.3"]
@@ -98,7 +98,9 @@ class BaseRepository(Generic[T, K]):
98
98
  treated as PostgreSQL arrays rather than JSONB. Only relevant when
99
99
  auto_detect_json=False. Defaults to None.
100
100
  vector_fields (Optional[Set[str]], optional): Explicit set of field names to treat
101
- as pgvector columns. If provided, overrides auto-detection. Defaults to None.
101
+ as pgvector columns. Vector fields are returned as strings from PostgreSQL and
102
+ automatically parsed to list[float] using JSON parsing. If provided, overrides
103
+ auto-detection. Defaults to None.
102
104
  auto_detect_vector (bool, optional): Whether to automatically detect vector fields
103
105
  (list[float]) from Pydantic type hints. Ignored if vector_fields is provided.
104
106
  Defaults to True.
@@ -153,35 +155,6 @@ class BaseRepository(Generic[T, K]):
153
155
  # Check if psycopg JSON adapters are enabled
154
156
  self._use_psycopg_adapters = self._check_psycopg_adapters()
155
157
 
156
- # Store flag for lazy registration of pgvector adapter
157
- self._vector_adapter_registered = False
158
- self._needs_vector_adapter = bool(self._vector_fields)
159
-
160
- async def _ensure_vector_adapter_registered(self):
161
- """Ensure pgvector adapter is registered on the connection.
162
-
163
- This is called lazily on first use to handle async registration properly.
164
- Uses the async variant of register_vector for async connections.
165
- """
166
- if not self._needs_vector_adapter or self._vector_adapter_registered:
167
- return
168
-
169
- try:
170
- from pgvector.psycopg import register_vector_async
171
-
172
- await register_vector_async(self.db_connection)
173
- self._vector_adapter_registered = True
174
- logger.info(f"Registered pgvector adapter for {self.table_name} with fields: {self._vector_fields}")
175
- except ImportError:
176
- logger.warning(
177
- f"pgvector package not installed but vector fields detected in {self.table_name}: {self._vector_fields}. "
178
- "Vector fields will be returned as strings. Install pgvector with: pip install pgvector"
179
- )
180
- self._needs_vector_adapter = False # Don't try again
181
- except Exception as e:
182
- logger.warning(f"Failed to register pgvector adapter for {self.table_name}: {e}")
183
- self._needs_vector_adapter = False # Don't try again
184
-
185
158
  def _check_psycopg_adapters(self) -> bool:
186
159
  """Check if psycopg JSON adapters are enabled on the connection.
187
160
 
@@ -348,6 +321,29 @@ class BaseRepository(Generic[T, K]):
348
321
  f"Converted date field '{field_name}' from {type(value).__name__} to ISO string for {self.table_name}"
349
322
  )
350
323
 
324
+ # Parse vector fields from PostgreSQL string format to Python list[float]
325
+ # PostgreSQL returns vectors as strings like '[0.1,0.2,0.3]'
326
+ for field_name in self._vector_fields:
327
+ if field_name in processed_data and processed_data[field_name] is not None:
328
+ value = processed_data[field_name]
329
+ if isinstance(value, str):
330
+ try:
331
+ # Parse string to list[float] using json.loads()
332
+ parsed_vector = JSONHandler.deserialize(value)
333
+ if isinstance(parsed_vector, list):
334
+ processed_data[field_name] = parsed_vector
335
+ logger.debug(
336
+ f"Parsed vector field '{field_name}' from string to list[float] for {self.table_name}"
337
+ )
338
+ else:
339
+ logger.warning(
340
+ f"Vector field '{field_name}' parsed to {type(parsed_vector).__name__} instead of list for {self.table_name}"
341
+ )
342
+ except Exception as e:
343
+ logger.warning(
344
+ f"Failed to parse vector field '{field_name}' in {self.table_name}: {e}. Keeping as string."
345
+ )
346
+
351
347
  # If no JSON fields should be processed, return the processed data
352
348
  if not json_fields:
353
349
  logger.debug(f"No JSON fields configured for {self.table_name}, skipping JSON postprocessing")
@@ -402,9 +398,6 @@ class BaseRepository(Generic[T, K]):
402
398
  If the model has JSON fields, they will be automatically serialized before
403
399
  insertion and deserialized when returning the created record.
404
400
  """
405
- # Ensure pgvector adapter is registered if needed
406
- await self._ensure_vector_adapter_registered()
407
-
408
401
  try:
409
402
  data = item.model_dump()
410
403
  # Preprocess data to serialize JSON fields
@@ -511,9 +504,6 @@ class BaseRepository(Generic[T, K]):
511
504
  If the model has JSON fields, they will be automatically deserialized
512
505
  from the database representation before creating the model instance.
513
506
  """
514
- # Ensure pgvector adapter is registered if needed
515
- await self._ensure_vector_adapter_registered()
516
-
517
507
  try:
518
508
  select_query = PsycopgHelper.build_select_query(self.table_name, where_clause={self.primary_key: record_id})
519
509
  async with self.db_connection.cursor(row_factory=dict_row) as cur:
@@ -556,9 +546,6 @@ class BaseRepository(Generic[T, K]):
556
546
  If the model has JSON fields, they will be automatically deserialized
557
547
  from the database representation before creating the model instances.
558
548
  """
559
- # Ensure pgvector adapter is registered if needed
560
- await self._ensure_vector_adapter_registered()
561
-
562
549
  try:
563
550
  async with self.db_connection.cursor(row_factory=dict_row) as cur:
564
551
  query = SQL("SELECT * FROM {}").format(Identifier(self.table_name))
File without changes