From d5c929722bfec6bb4268d5430d62c69792582098 Mon Sep 17 00:00:00 2001 From: jinliyl <6469360+jinliyl@users.noreply.github.com> Date: Tue, 31 Mar 2026 20:59:59 +0800 Subject: [PATCH] refactor(file_store): move sqlite3 imports inside initialization methods (#193) * fix(core): handle chromadb import error gracefully - Changed CHROMADB_AVAILABLE flag to _CHROMADB_IMPORT_ERROR exception storage - Updated version from 0.3.1.7 to 0.3.1.8 - Modified import error handling to preserve original exception details - Removed hardcoded ImportError message in favor of dynamic exception raising - Added proper logger initialization using get_logger utility * refactor(file_store): move sqlite3 imports inside initialization methods - Moved sqlite3 import from module level to inside init methods - Removed unused import statement at top of file - Maintains same functionality while improving import organization - Prevents potential issues with early sqlite3 dependency loading * refactor(core): update import error handling with broader exception types - Changed ImportError to Exception for ray import error handling - Updated chromadb import error to use Exception instead of ImportError - Modified elasticsearch import error to catch general exceptions - Changed asyncpg import error handling from ImportError to Exception - Updated qdrant import error to use Exception instead of ImportError - Added explicit type hints for all import error variables as Exception | None --- reme/core/file_store/chroma_file_store.py | 4 ++-- reme/core/file_store/sqlite_file_store.py | 4 +++- reme/core/op/base_ray_op.py | 4 ++-- reme/core/vector_store/chroma_vector_store.py | 4 ++-- reme/core/vector_store/es_vector_store.py | 4 ++-- reme/core/vector_store/pgvector_store.py | 4 ++-- reme/core/vector_store/qdrant_vector_store.py | 4 ++-- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/reme/core/file_store/chroma_file_store.py b/reme/core/file_store/chroma_file_store.py index 9892f7d1..6dda41b8 100644 --- a/reme/core/file_store/chroma_file_store.py +++ b/reme/core/file_store/chroma_file_store.py @@ -16,8 +16,8 @@ try: import chromadb from chromadb.config import Settings - _CHROMADB_IMPORT_ERROR: ImportError | None = None -except ImportError as e: + _CHROMADB_IMPORT_ERROR: Exception | None = None +except Exception as e: _CHROMADB_IMPORT_ERROR = e chromadb = None Settings = None diff --git a/reme/core/file_store/sqlite_file_store.py b/reme/core/file_store/sqlite_file_store.py index 7fd49762..0a494d2c 100644 --- a/reme/core/file_store/sqlite_file_store.py +++ b/reme/core/file_store/sqlite_file_store.py @@ -1,7 +1,7 @@ """SQLite storage backend for file store.""" import json -import sqlite3 + import struct import time @@ -29,6 +29,7 @@ class SqliteFileStore(BaseFileStore): def __init__(self, vec_ext_path: str = "", **kwargs): super().__init__(**kwargs) self.vec_ext_path = vec_ext_path + import sqlite3 self.conn: sqlite3.Connection | None = None @@ -61,6 +62,7 @@ class SqliteFileStore(BaseFileStore): """Initialize database and load extensions.""" if self.conn is not None: return + import sqlite3 self.conn = sqlite3.connect(self.db_path / "reme.db", check_same_thread=False) diff --git a/reme/core/op/base_ray_op.py b/reme/core/op/base_ray_op.py index f63e7b83..2086d953 100644 --- a/reme/core/op/base_ray_op.py +++ b/reme/core/op/base_ray_op.py @@ -10,11 +10,11 @@ from tqdm import tqdm from .base_op import BaseOp from ..base_dict import BaseDict -_RAY_IMPORT_ERROR = None +_RAY_IMPORT_ERROR: Exception | None = None try: import ray -except ImportError as _e: +except Exception as _e: _RAY_IMPORT_ERROR = _e ray = None diff --git a/reme/core/vector_store/chroma_vector_store.py b/reme/core/vector_store/chroma_vector_store.py index 2bd9f4ae..c6bfc1c9 100644 --- a/reme/core/vector_store/chroma_vector_store.py +++ b/reme/core/vector_store/chroma_vector_store.py @@ -9,12 +9,12 @@ from .base_vector_store import BaseVectorStore from ..embedding import BaseEmbeddingModel from ..schema import VectorNode -_CHROMADB_IMPORT_ERROR = None +_CHROMADB_IMPORT_ERROR: Exception | None = None try: import chromadb from chromadb.config import Settings -except ImportError as e: +except Exception as e: _CHROMADB_IMPORT_ERROR = e chromadb = None Settings = None diff --git a/reme/core/vector_store/es_vector_store.py b/reme/core/vector_store/es_vector_store.py index e0df83a0..332bfa68 100644 --- a/reme/core/vector_store/es_vector_store.py +++ b/reme/core/vector_store/es_vector_store.py @@ -13,12 +13,12 @@ from .base_vector_store import BaseVectorStore from ..embedding import BaseEmbeddingModel from ..schema import VectorNode -_ELASTICSEARCH_IMPORT_ERROR = None +_ELASTICSEARCH_IMPORT_ERROR: Exception | None = None try: from elasticsearch import AsyncElasticsearch from elasticsearch.helpers import async_bulk -except ImportError as e: +except Exception as e: _ELASTICSEARCH_IMPORT_ERROR = e AsyncElasticsearch = None async_bulk = None diff --git a/reme/core/vector_store/pgvector_store.py b/reme/core/vector_store/pgvector_store.py index bc665cd9..03805076 100644 --- a/reme/core/vector_store/pgvector_store.py +++ b/reme/core/vector_store/pgvector_store.py @@ -11,12 +11,12 @@ from .base_vector_store import BaseVectorStore from ..embedding import BaseEmbeddingModel from ..schema import VectorNode -_ASYNCPG_IMPORT_ERROR = None +_ASYNCPG_IMPORT_ERROR: Exception | None = None try: import asyncpg from asyncpg import Pool -except ImportError as e: +except Exception as e: _ASYNCPG_IMPORT_ERROR = e asyncpg = None Pool = None diff --git a/reme/core/vector_store/qdrant_vector_store.py b/reme/core/vector_store/qdrant_vector_store.py index b981a23f..3e69d667 100644 --- a/reme/core/vector_store/qdrant_vector_store.py +++ b/reme/core/vector_store/qdrant_vector_store.py @@ -9,7 +9,7 @@ from .base_vector_store import BaseVectorStore from ..embedding import BaseEmbeddingModel from ..schema import VectorNode -_QDRANT_IMPORT_ERROR = None +_QDRANT_IMPORT_ERROR: Exception | None = None try: from qdrant_client import AsyncQdrantClient @@ -23,7 +23,7 @@ try: Range, VectorParams, ) -except ImportError as e: +except Exception as e: _QDRANT_IMPORT_ERROR = e AsyncQdrantClient = None Distance = None