From 2b08e9e26561c7cfa94037b0d5e88ed63071986f Mon Sep 17 00:00:00 2001 From: ccurme Date: Wed, 19 Jun 2024 11:24:38 -0400 Subject: [PATCH] core[patch]: update test to catch circular imports (#23172) This raises ImportError due to a circular import: ```python from langchain_core import chat_history ``` This does not: ```python from langchain_core import runnables from langchain_core import chat_history ``` Here we update `test_imports` to run each import in a separate subprocess. Open to other ways of doing this! --- libs/core/langchain_core/runnables/__init__.py | 2 -- libs/core/tests/unit_tests/runnables/test_imports.py | 1 - libs/core/tests/unit_tests/test_imports.py | 12 ++++++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libs/core/langchain_core/runnables/__init__.py b/libs/core/langchain_core/runnables/__init__.py index 518d5c38c9..9c4362c9c8 100644 --- a/libs/core/langchain_core/runnables/__init__.py +++ b/libs/core/langchain_core/runnables/__init__.py @@ -36,7 +36,6 @@ from langchain_core.runnables.config import ( run_in_executor, ) from langchain_core.runnables.fallbacks import RunnableWithFallbacks -from langchain_core.runnables.history import RunnableWithMessageHistory from langchain_core.runnables.passthrough import ( RunnableAssign, RunnablePassthrough, @@ -79,7 +78,6 @@ __all__ = [ "RunnablePick", "RunnableSequence", "RunnableWithFallbacks", - "RunnableWithMessageHistory", "get_config_list", "aadd", "add", diff --git a/libs/core/tests/unit_tests/runnables/test_imports.py b/libs/core/tests/unit_tests/runnables/test_imports.py index 12b1a80d1b..48098aa6db 100644 --- a/libs/core/tests/unit_tests/runnables/test_imports.py +++ b/libs/core/tests/unit_tests/runnables/test_imports.py @@ -26,7 +26,6 @@ EXPECTED_ALL = [ "RunnablePick", "RunnableSequence", "RunnableWithFallbacks", - "RunnableWithMessageHistory", "get_config_list", "aadd", "add", diff --git a/libs/core/tests/unit_tests/test_imports.py b/libs/core/tests/unit_tests/test_imports.py index d4b49d2392..7d64113378 100644 --- a/libs/core/tests/unit_tests/test_imports.py +++ b/libs/core/tests/unit_tests/test_imports.py @@ -1,7 +1,10 @@ import glob import importlib +import subprocess from pathlib import Path +import pytest + def test_importable_all() -> None: for path in glob.glob("../core/langchain_core/*"): @@ -13,3 +16,12 @@ def test_importable_all() -> None: all_ = getattr(module, "__all__", []) for cls_ in all_: getattr(module, cls_) + + # Test import in isolation + # Note: ImportErrors due to circular imports can be raised + # for one sequence of imports but not another. + result = subprocess.run( + ["python", "-c", f"import langchain_core.{module_name}"], + ) + if result.returncode != 0: + pytest.fail(f"Failed to import {module_name}.")