diff --git a/libs/experimental/tests/unit_tests/test_imports.py b/libs/experimental/tests/unit_tests/test_imports.py index 8db98f5c2de..7da7cb3f8fc 100644 --- a/libs/experimental/tests/unit_tests/test_imports.py +++ b/libs/experimental/tests/unit_tests/test_imports.py @@ -1,15 +1,46 @@ -import glob import importlib from pathlib import Path +PKG_ROOT = Path(__file__).parent.parent.parent +PKG_CODE = PKG_ROOT / "langchain_experimental" + def test_importable_all() -> None: - for path in glob.glob("../experimental/langchain_experimental/*"): - relative_path = Path(path).parts[-1] + """Test that all modules in langchain_experimental are importable.""" + failures = [] + found_at_least_one = False + for path in PKG_CODE.rglob("*.py"): + relative_path = str(Path(path).relative_to(PKG_CODE)).replace("/", ".") if relative_path.endswith(".typed"): continue - module_name = relative_path.split(".")[0] - module = importlib.import_module("langchain_experimental." + module_name) + if relative_path.endswith("/__init__.py"): + # Then strip __init__.py + s = "/__init__.py" + module_name = relative_path[: -len(s)] + else: # just strip .py + module_name = relative_path[:-3] + + if not module_name: + continue + try: + module = importlib.import_module("langchain_experimental." + module_name) + except ImportError: + failures.append("langchain_experimental." + module_name) + continue + all_ = getattr(module, "__all__", []) for cls_ in all_: - getattr(module, cls_) + try: + getattr(module, cls_) + except AttributeError: + failures.append(f"{module_name}.{cls_}") + + found_at_least_one = True + + if failures: + raise AssertionError( + "The following modules or classes could not be imported: " + + ", ".join(failures) + ) + + assert found_at_least_one is True