Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(vector-store): keep WeaviateStore usable with weaviate-client installed (#2125)
张梁2ef2429e6bf1
diff --git a/semantica/vector_store/weaviate_store.py b/semantica/vector_store/weaviate_store.pyindex baf5270b..8ebb95b9 100644--- a/semantica/vector_store/weaviate_store.py+++ b/semantica/vector_store/weaviate_store.py@@ -45,14 +45,19 @@ from ..utils.progress_tracker import get_progress_tracker# Optional Weaviate importtry:import weaviate- from weaviate.classes.query import MetadataQuery, QueryReturn++ # ``QueryReturn`` used to be imported here next to ``MetadataQuery`` even+ # though nothing in this module ever used it. weaviate-client 4.16 dropped+ # the name, and the resulting ImportError marked the whole optional+ # dependency as missing: every WeaviateStore method then raised+ # "Weaviate not available" on an installation where weaviate-client works.+ from weaviate.classes.query import MetadataQueryWEAVIATE_AVAILABLE = Trueexcept (ImportError, OSError):WEAVIATE_AVAILABLE = Falseweaviate = NoneMetadataQuery = None- QueryReturn = Noneclass WeaviateClient:diff --git a/tests/vector_store/test_weaviate_store.py b/tests/vector_store/test_weaviate_store.pyindex ccde42a4..abc0bec9 100644--- a/tests/vector_store/test_weaviate_store.py+++ b/tests/vector_store/test_weaviate_store.py@@ -5,6 +5,9 @@ WeaviateStore against MagicMocks, following the pattern already used forweaviate in test_backend_metadata_filtering.py."""+import importlib+import sys+import typesfrom unittest.mock import MagicMock, patchimport numpy as np@@ -258,3 +261,55 @@ def test_iter_all_propagates_fetch_errors():with pytest.raises(RuntimeError, match="connection reset"):list(store.iter_all())+++def _fake_weaviate(*, include_query_return: bool) -> dict[str, types.ModuleType]:+ """Build a minimal stand-in for the ``weaviate`` package tree.++ weaviate-client is not a test dependency here, so a real import cannot reach+ the "4.16 removed QueryReturn" path. This fake module tree feeds it to+ ``importlib.reload`` instead.+ """+ weaviate = types.ModuleType("weaviate")+ classes = types.ModuleType("weaviate.classes")+ query = types.ModuleType("weaviate.classes.query")+ query.MetadataQuery = MagicMock(name="MetadataQuery")+ if include_query_return:+ query.QueryReturn = MagicMock(name="QueryReturn")+ classes.query = query+ weaviate.classes = classes+ return {+ "weaviate": weaviate,+ "weaviate.classes": classes,+ "weaviate.classes.query": query,+ }++[email protected]("include_query_return", [True, False])+def test_query_return_is_not_a_hard_requirement(include_query_return):+ """weaviate-client 4.16 removed ``QueryReturn``.++ The module used to import the name without ever using it, so removing it+ must not flip WEAVIATE_AVAILABLE to False: that marks the whole optional+ dependency as missing and makes every WeaviateStore method raise "Weaviate+ not available" even though weaviate-client is installed.+ """+ import semantica.vector_store.weaviate_store as store_module++ fake_modules = _fake_weaviate(include_query_return=include_query_return)+ saved = {name: sys.modules.get(name) for name in fake_modules}+ try:+ with patch.dict(sys.modules, fake_modules):+ reloaded = importlib.reload(store_module)+ assert reloaded.WEAVIATE_AVAILABLE is True+ assert (+ reloaded.MetadataQuery+ is fake_modules["weaviate.classes.query"].MetadataQuery+ )+ finally:+ for name, module in saved.items():+ if module is None:+ sys.modules.pop(name, None)+ else:+ sys.modules[name] = module+ importlib.reload(store_module)