core[patch]: Speed up unit tests for imports (#23837)

Speed up unit tests for imports
This commit is contained in:
Eugene Yurtsev 2024-07-03 15:55:15 -04:00 committed by GitHub
parent 4a15fce516
commit 4ab78572e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,9 @@
import concurrent.futures
import glob import glob
import importlib import importlib
import subprocess import subprocess
from pathlib import Path from pathlib import Path
from typing import Tuple
import pytest
def test_importable_all() -> None: def test_importable_all() -> None:
@ -17,11 +17,40 @@ def test_importable_all() -> None:
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 def try_to_import(module_name: str) -> Tuple[int, str]:
# for one sequence of imports but not another. """Try to import a module via subprocess."""
module = importlib.import_module("langchain_core." + module_name)
all_ = getattr(module, "__all__", [])
for cls_ in all_:
getattr(module, cls_)
result = subprocess.run( result = subprocess.run(
["python", "-c", f"import langchain_core.{module_name}"], ["python", "-c", f"import langchain_core.{module_name}"],
) )
if result.returncode != 0: return result.returncode, module_name
pytest.fail(f"Failed to import {module_name}.")
def test_importable_all_via_subprocess() -> None:
"""Test import in isolation.
Note: ImportErrors due to circular imports can be raised
for one sequence of imports but not another.
"""
module_names = []
for path in glob.glob("../core/langchain_core/*"):
relative_path = Path(path).parts[-1]
if relative_path.endswith(".typed"):
continue
module_name = relative_path.split(".")[0]
module_names.append(module_name)
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
futures = [
executor.submit(try_to_import, module_name) for module_name in module_names
]
for future in concurrent.futures.as_completed(futures):
result = future.result() # Will raise an exception if the callable raised
code, module_name = result
if code != 0:
raise ValueError(f"Failed to import {module_name}.")