Skip to content

The agent engineering platform.

infra: import CI speed (#14566)

Erick Friis482e2b94fa5e
diff --git a/libs/community/Makefile b/libs/community/Makefile
index 68d8feee95..203c028e3e 100644
--- a/libs/community/Makefile
+++ b/libs/community/Makefile
@@ -16,9 +16,7 @@ test_watch:
poetry run ptw --snapshot-update --now . -- -vv -x tests/unit_tests
check_imports: $(shell find langchain_community -name '*.py')
- for f in $^ ; do \
- python -c "from importlib.machinery import SourceFileLoader; SourceFileLoader('x', '$$f').load_module()" || exit 1; \
- done
+ poetry run python ./scripts/check_imports.py $^
extended_tests:
poetry run pytest --only-extended tests/unit_tests
@@ -39,7 +37,7 @@ lint_tests: MYPY_CACHE=.mypy_cache_test
lint lint_diff lint_package lint_tests:
./scripts/check_pydantic.sh .
- ./scripts/check_imports.sh
+ ./scripts/lint_imports.sh
poetry run ruff .
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff format $(PYTHON_FILES) --diff
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff --select I $(PYTHON_FILES)
diff --git a/libs/community/scripts/check_imports.py b/libs/community/scripts/check_imports.py
new file mode 100644
index 0000000000..fd21a4975b
--- /dev/null
+++ b/libs/community/scripts/check_imports.py
@@ -0,0 +1,17 @@
+import sys
+import traceback
+from importlib.machinery import SourceFileLoader
+
+if __name__ == "__main__":
+ files = sys.argv[1:]
+ has_failure = False
+ for file in files:
+ try:
+ SourceFileLoader("x", file).load_module()
+ except Exception:
+ has_faillure = True
+ print(file)
+ traceback.print_exc()
+ print()
+
+ sys.exit(1 if has_failure else 0)
diff --git a/libs/community/scripts/check_imports.sh b/libs/community/scripts/check_imports.sh
deleted file mode 100755
index 8da63d925a..0000000000
--- a/libs/community/scripts/check_imports.sh
+++ /dev/null
@@ -1,17 +0,0 @@
-#!/bin/bash
-
-set -eu
-
-# Initialize a variable to keep track of errors
-errors=0
-
-# make sure not importing from langchain_community or langchain_experimental
-git --no-pager grep '^from langchain\.' . && errors=$((errors+1))
-git --no-pager grep '^from langchain_experimental\.' . && errors=$((errors+1))
-
-# Decide on an exit status based on the errors
-if [ "$errors" -gt 0 ]; then
- exit 1
-else
- exit 0
-fi
diff --git a/libs/core/Makefile b/libs/core/Makefile
index 9e4c2a8525..2c685dfd44 100644
--- a/libs/core/Makefile
+++ b/libs/core/Makefile
@@ -16,9 +16,7 @@ test_watch:
poetry run ptw --snapshot-update --now . -- -vv -x tests/unit_tests
check_imports: $(shell find langchain_core -name '*.py')
- for f in $^ ; do \
- python -c "from importlib.machinery import SourceFileLoader; SourceFileLoader('x', '$$f').load_module()" || exit 1; \
- done
+ poetry run python ./scripts/check_imports.py $^
extended_tests:
poetry run pytest --only-extended $(TEST_FILE)
diff --git a/libs/core/scripts/check_imports.py b/libs/core/scripts/check_imports.py
new file mode 100644
index 0000000000..fd21a4975b
--- /dev/null
+++ b/libs/core/scripts/check_imports.py
@@ -0,0 +1,17 @@
+import sys
+import traceback
+from importlib.machinery import SourceFileLoader
+
+if __name__ == "__main__":
+ files = sys.argv[1:]
+ has_failure = False
+ for file in files:
+ try:
+ SourceFileLoader("x", file).load_module()
+ except Exception:
+ has_faillure = True
+ print(file)
+ traceback.print_exc()
+ print()
+
+ sys.exit(1 if has_failure else 0)
diff --git a/libs/experimental/Makefile b/libs/experimental/Makefile
index eb20ffc13b..623658354c 100644
--- a/libs/experimental/Makefile
+++ b/libs/experimental/Makefile
@@ -22,9 +22,7 @@ integration_tests:
poetry run pytest tests/integration_tests
check_imports: $(shell find langchain_experimental -name '*.py')
- for f in $^ ; do \
- python -c "from importlib.machinery import SourceFileLoader; SourceFileLoader('x', '$$f').load_module()" || exit 1; \
- done
+ poetry run python ./scripts/check_imports.py $^
######################
diff --git a/libs/experimental/scripts/check_imports.py b/libs/experimental/scripts/check_imports.py
new file mode 100644
index 0000000000..fd21a4975b
--- /dev/null
+++ b/libs/experimental/scripts/check_imports.py
@@ -0,0 +1,17 @@
+import sys
+import traceback
+from importlib.machinery import SourceFileLoader
+
+if __name__ == "__main__":
+ files = sys.argv[1:]
+ has_failure = False
+ for file in files:
+ try:
+ SourceFileLoader("x", file).load_module()
+ except Exception:
+ has_faillure = True
+ print(file)
+ traceback.print_exc()
+ print()
+
+ sys.exit(1 if has_failure else 0)
diff --git a/libs/langchain/Makefile b/libs/langchain/Makefile
index 3a1daeae97..50f1e423c0 100644
--- a/libs/langchain/Makefile
+++ b/libs/langchain/Makefile
@@ -41,9 +41,7 @@ docker_tests:
docker run --rm my-langchain-image:test
check_imports: $(shell find langchain -name '*.py')
- for f in $^ ; do \
- python -c "from importlib.machinery import SourceFileLoader; SourceFileLoader('x', '$$f').load_module()" || exit 1; \
- done
+ poetry run python ./scripts/check_imports.py $^
######################
# LINTING AND FORMATTING
diff --git a/libs/langchain/scripts/check_imports.py b/libs/langchain/scripts/check_imports.py
new file mode 100644
index 0000000000..fd21a4975b
--- /dev/null
+++ b/libs/langchain/scripts/check_imports.py
@@ -0,0 +1,17 @@
+import sys
+import traceback
+from importlib.machinery import SourceFileLoader
+
+if __name__ == "__main__":
+ files = sys.argv[1:]
+ has_failure = False
+ for file in files:
+ try:
+ SourceFileLoader("x", file).load_module()
+ except Exception:
+ has_faillure = True
+ print(file)
+ traceback.print_exc()
+ print()
+
+ sys.exit(1 if has_failure else 0)