Skip to content

Commit

Permalink
core[patch]: Speed up unit tests for imports (#23837)
Browse files Browse the repository at this point in the history
Speed up unit tests for imports
  • Loading branch information
eyurtsev authored Jul 3, 2024
1 parent 4a15fce commit 4ab7857
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions libs/core/tests/unit_tests/test_imports.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import concurrent.futures
import glob
import importlib
import subprocess
from pathlib import Path

import pytest
from typing import Tuple


def test_importable_all() -> None:
Expand All @@ -17,11 +17,40 @@ def test_importable_all() -> None:
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}.")

def try_to_import(module_name: str) -> Tuple[int, str]:
"""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(
["python", "-c", f"import langchain_core.{module_name}"],
)
return result.returncode, 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}.")

0 comments on commit 4ab7857

Please sign in to comment.