Skip to content

Commit 6b6e54c

Browse files
dkaushik94claude
andcommitted
test(lfx): guard knowledge-deps probe against the chroma sys.modules shim
The module-level ``find_spec("langchain_chroma")`` crashed collection of the whole lfx suite with ``ValueError: langchain_chroma.__spec__ is None``. The KB-backends conftest registers a bare ``types.ModuleType("langchain_chroma")`` shim into ``sys.modules`` when the real package is absent; that shim satisfies the knowledge module's ``from langchain_chroma import Chroma`` (so it imports), but its ``__spec__`` is None, which makes ``find_spec`` raise rather than return a spec. Running the file alone passed (no conftest, no shim); the full ``make lfx_tests`` failed. Probe via a helper that checks ``sys.modules`` first and guards ``find_spec`` with ``except (ImportError, ValueError)`` — the same pattern the conftest's own ``_is_missing`` uses — so a shimmed, a real, and an absent langchain_chroma all resolve correctly. Not flakiness and nothing was removed: purely the probe tripping over another suite's fixture. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 0ce85f6 commit 6b6e54c

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

src/lfx/tests/unit/custom/component/test_dynamic_imports.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,41 @@
1010
"""
1111

1212
import importlib.util
13+
import sys
1314
from unittest.mock import patch
1415

1516
import pytest
1617
from lfx.components._importing import import_mod
1718

18-
# ``files_and_knowledge.knowledge`` (KnowledgeComponent) imports ``langchain_chroma``
19-
# at module-import time; every ``langflow`` import in it is lazy. So whether the
20-
# module — and the component class — imports cleanly depends solely on that one
21-
# optional dependency being installed. The engine-only lfx test env normally lacks
22-
# it, but some CI environments carry it transitively, so these tests branch on its
23-
# presence rather than hard-assuming it is absent (mirrors ``test_type_checking_imports``).
24-
_KNOWLEDGE_DEPS_AVAILABLE = importlib.util.find_spec("langchain_chroma") is not None
19+
20+
def _knowledge_deps_available() -> bool:
21+
"""Whether the knowledge module's ``langchain_chroma`` import can resolve.
22+
23+
``files_and_knowledge.knowledge`` (KnowledgeComponent) imports
24+
``langchain_chroma`` at module-import time; every ``langflow`` import in it
25+
is lazy. So whether that module — and the component class — imports cleanly
26+
depends solely on this one optional dependency being importable. The
27+
engine-only lfx test env normally lacks it, but some CI environments carry
28+
it transitively, so these tests branch on its presence rather than
29+
hard-assuming it is absent (mirrors ``test_type_checking_imports``).
30+
31+
Must tolerate the KB-backends conftest, which registers a bare
32+
``types.ModuleType("langchain_chroma")`` shim into ``sys.modules`` when the
33+
real package is missing. That shim still satisfies ``from langchain_chroma
34+
import Chroma`` — so the knowledge module imports — but its ``__spec__`` is
35+
``None``, which makes ``find_spec`` raise ``ValueError``. Check
36+
``sys.modules`` first, and guard ``find_spec`` the same way the conftest's
37+
own ``_is_missing`` helper does.
38+
"""
39+
if "langchain_chroma" in sys.modules:
40+
return True
41+
try:
42+
return importlib.util.find_spec("langchain_chroma") is not None
43+
except (ImportError, ValueError):
44+
return False
45+
46+
47+
_KNOWLEDGE_DEPS_AVAILABLE = _knowledge_deps_available()
2548

2649

2750
class TestImportUtils:

0 commit comments

Comments
 (0)