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!
This commit is contained in:
ccurme 2024-06-19 11:24:38 -04:00 committed by GitHub
parent ae4c0ed25a
commit 2b08e9e265
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 3 deletions

View File

@ -36,7 +36,6 @@ from langchain_core.runnables.config import (
run_in_executor, run_in_executor,
) )
from langchain_core.runnables.fallbacks import RunnableWithFallbacks from langchain_core.runnables.fallbacks import RunnableWithFallbacks
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_core.runnables.passthrough import ( from langchain_core.runnables.passthrough import (
RunnableAssign, RunnableAssign,
RunnablePassthrough, RunnablePassthrough,
@ -79,7 +78,6 @@ __all__ = [
"RunnablePick", "RunnablePick",
"RunnableSequence", "RunnableSequence",
"RunnableWithFallbacks", "RunnableWithFallbacks",
"RunnableWithMessageHistory",
"get_config_list", "get_config_list",
"aadd", "aadd",
"add", "add",

View File

@ -26,7 +26,6 @@ EXPECTED_ALL = [
"RunnablePick", "RunnablePick",
"RunnableSequence", "RunnableSequence",
"RunnableWithFallbacks", "RunnableWithFallbacks",
"RunnableWithMessageHistory",
"get_config_list", "get_config_list",
"aadd", "aadd",
"add", "add",

View File

@ -1,7 +1,10 @@
import glob import glob
import importlib import importlib
import subprocess
from pathlib import Path from pathlib import Path
import pytest
def test_importable_all() -> None: def test_importable_all() -> None:
for path in glob.glob("../core/langchain_core/*"): for path in glob.glob("../core/langchain_core/*"):
@ -13,3 +16,12 @@ def test_importable_all() -> None:
all_ = getattr(module, "__all__", []) all_ = getattr(module, "__all__", [])
for cls_ in all_: for cls_ in all_:
getattr(module, cls_) 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}.")