Skip to content

The agent engineering platform.

langchain[minor]: enhance unit test to test imports recursively (#21122)

Eugene Yurtsevbf9541475897
diff --git a/libs/langchain/langchain/chains/llm_bash/__init__.py b/libs/langchain/langchain/chains/llm_bash/__init__.py
index cb6b48377d..b055d992e4 100644
--- a/libs/langchain/langchain/chains/llm_bash/__init__.py
+++ b/libs/langchain/langchain/chains/llm_bash/__init__.py
@@ -1,6 +1,6 @@
def __getattr__(name: str = "") -> None:
"""Raise an error on import since is deprecated."""
- raise ImportError(
+ raise AttributeError(
"This module has been moved to langchain-experimental. "
"For more details: https://github.com/langchain-ai/langchain/discussions/11352."
"To access this code, install it with `pip install langchain-experimental`."
diff --git a/libs/langchain/langchain/chains/llm_symbolic_math/__init__.py b/libs/langchain/langchain/chains/llm_symbolic_math/__init__.py
index 640a8aa909..a3ce265f06 100644
--- a/libs/langchain/langchain/chains/llm_symbolic_math/__init__.py
+++ b/libs/langchain/langchain/chains/llm_symbolic_math/__init__.py
@@ -1,6 +1,6 @@
def __getattr__(name: str = "") -> None:
"""Raise an error on import since is deprecated."""
- raise ImportError(
+ raise AttributeError(
"This module has been moved to langchain-experimental. "
"For more details: https://github.com/langchain-ai/langchain/discussions/11352."
"To access this code, install it with `pip install langchain-experimental`."
diff --git a/libs/langchain/langchain/tools/python/__init__.py b/libs/langchain/langchain/tools/python/__init__.py
index a7f4082d30..9c60cf9c94 100644
--- a/libs/langchain/langchain/tools/python/__init__.py
+++ b/libs/langchain/langchain/tools/python/__init__.py
@@ -2,7 +2,7 @@ from typing import Any
def __getattr__(name: str = "") -> Any:
- raise ImportError(
+ raise AttributeError(
"This tool has been moved to langchain experiment. "
"This tool has access to a python REPL. "
"For best practices make sure to sandbox this tool. "
diff --git a/libs/langchain/tests/unit_tests/test_imports.py b/libs/langchain/tests/unit_tests/test_imports.py
index 5f93441efe..62e17021d6 100644
--- a/libs/langchain/tests/unit_tests/test_imports.py
+++ b/libs/langchain/tests/unit_tests/test_imports.py
@@ -1,15 +1,27 @@
-import glob
import importlib
from pathlib import Path
+# Attempt to recursively import all modules in langchain
+PKG_ROOT = Path(__file__).parent.parent.parent
-def test_importable_all() -> None:
- for path in glob.glob("../langchain/langchain/*"):
- relative_path = Path(path).parts[-1]
- if relative_path.endswith(".typed"):
- continue
- module_name = relative_path.split(".")[0]
- module = importlib.import_module("langchain." + module_name)
- all_ = getattr(module, "__all__", [])
- for cls_ in all_:
- getattr(module, cls_)
+
+def test_import_all() -> None:
+ """Generate the public API for this package."""
+ library_code = PKG_ROOT / "langchain"
+ for path in library_code.rglob("*.py"):
+ # Calculate the relative path to the module
+ module_name = (
+ path.relative_to(PKG_ROOT).with_suffix("").as_posix().replace("/", ".")
+ )
+ if module_name.endswith("__init__"):
+ # Without init
+ module_name = module_name.rsplit(".", 1)[0]
+
+ mod = importlib.import_module(module_name)
+
+ all = getattr(mod, "__all__", [])
+
+ for name in all:
+ # Attempt to import the name from the module
+ obj = getattr(mod, name)
+ assert obj is not None