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

This commit is contained in:
Eugene Yurtsev 2024-04-30 17:05:53 -04:00 committed by GitHub
parent e4f51f59a2
commit bf95414758
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 14 deletions

View File

@ -1,6 +1,6 @@
def __getattr__(name: str = "") -> None: def __getattr__(name: str = "") -> None:
"""Raise an error on import since is deprecated.""" """Raise an error on import since is deprecated."""
raise ImportError( raise AttributeError(
"This module has been moved to langchain-experimental. " "This module has been moved to langchain-experimental. "
"For more details: https://github.com/langchain-ai/langchain/discussions/11352." "For more details: https://github.com/langchain-ai/langchain/discussions/11352."
"To access this code, install it with `pip install langchain-experimental`." "To access this code, install it with `pip install langchain-experimental`."

View File

@ -1,6 +1,6 @@
def __getattr__(name: str = "") -> None: def __getattr__(name: str = "") -> None:
"""Raise an error on import since is deprecated.""" """Raise an error on import since is deprecated."""
raise ImportError( raise AttributeError(
"This module has been moved to langchain-experimental. " "This module has been moved to langchain-experimental. "
"For more details: https://github.com/langchain-ai/langchain/discussions/11352." "For more details: https://github.com/langchain-ai/langchain/discussions/11352."
"To access this code, install it with `pip install langchain-experimental`." "To access this code, install it with `pip install langchain-experimental`."

View File

@ -2,7 +2,7 @@ from typing import Any
def __getattr__(name: str = "") -> Any: def __getattr__(name: str = "") -> Any:
raise ImportError( raise AttributeError(
"This tool has been moved to langchain experiment. " "This tool has been moved to langchain experiment. "
"This tool has access to a python REPL. " "This tool has access to a python REPL. "
"For best practices make sure to sandbox this tool. " "For best practices make sure to sandbox this tool. "

View File

@ -1,15 +1,27 @@
import glob
import importlib import importlib
from pathlib import Path 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/*"): def test_import_all() -> None:
relative_path = Path(path).parts[-1] """Generate the public API for this package."""
if relative_path.endswith(".typed"): library_code = PKG_ROOT / "langchain"
continue for path in library_code.rglob("*.py"):
module_name = relative_path.split(".")[0] # Calculate the relative path to the module
module = importlib.import_module("langchain." + module_name) module_name = (
all_ = getattr(module, "__all__", []) path.relative_to(PKG_ROOT).with_suffix("").as_posix().replace("/", ".")
for cls_ in all_: )
getattr(module, cls_) 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