From b923951062791e50eef9677e032498e3ff3bcecf Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Mon, 13 May 2024 14:51:25 -0400 Subject: [PATCH] langchain[patch]: CI add lint rule for community imports (#21618) Add a rule to check for imports from community in global scope --- libs/langchain/scripts/lint_imports.sh | 6 ++++++ libs/langchain/tests/unit_tests/load/test_load.py | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libs/langchain/scripts/lint_imports.sh b/libs/langchain/scripts/lint_imports.sh index 5716202ab3d..1ab9dd7a733 100755 --- a/libs/langchain/scripts/lint_imports.sh +++ b/libs/langchain/scripts/lint_imports.sh @@ -27,6 +27,12 @@ langchain.(pydantic_v1|utils|schema|load|callbacks|env|_api|storage|llms|docstor # make sure not importing from langchain_experimental git --no-pager grep '^from langchain_experimental\.' . && errors=$((errors+1)) +# Add a basic lint rule to prevent imports from the global namespaces of langchain_community +# This lint rule won't catch imports from local scope. +# We can't add that rule without a more complex script to ignore imports from inside +# a if TYPE_CHECKING block. +git grep '^from langchain_community' | grep -vE '# ignore: community-import' && errors=$((errors+1)) + # Decide on an exit status based on the errors if [ "$errors" -gt 0 ]; then exit 1 diff --git a/libs/langchain/tests/unit_tests/load/test_load.py b/libs/langchain/tests/unit_tests/load/test_load.py index beb303635e6..16d4a7b3b07 100644 --- a/libs/langchain/tests/unit_tests/load/test_load.py +++ b/libs/langchain/tests/unit_tests/load/test_load.py @@ -12,7 +12,9 @@ pytest.importorskip( ) -from langchain_community.llms.openai import OpenAI as CommunityOpenAI # noqa: E402 +from langchain_community.llms.openai import ( # noqa: E402, # ignore: community-import + OpenAI as CommunityOpenAI, +) class NotSerializable: