Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure importability of modules not included in __init__.py files #5965

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/backend/base/langflow/utils/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,13 @@ def prepare_global_scope(module):
warnings.simplefilter("ignore", LangChainDeprecationWarning)
imported_module = importlib.import_module(node.module)
for alias in node.names:
exec_globals[alias.name] = getattr(imported_module, alias.name)
try:
# First try getting it as an attribute
exec_globals[alias.name] = getattr(imported_module, alias.name)
except AttributeError:
# If that fails, try importing the full module path
full_module_path = f"{node.module}.{alias.name}"
exec_globals[alias.name] = importlib.import_module(full_module_path)
except ModuleNotFoundError as e:
msg = f"Module {node.module} not found. Please install it and try again"
raise ModuleNotFoundError(msg) from e
Expand Down
20 changes: 20 additions & 0 deletions src/backend/tests/unit/test_validate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ def build(self):
assert result.value == "test"


def test_create_class_module_import():
code = """
from langflow.custom import CustomComponent
from PIL import ImageDraw

class ExternalClass:
def __init__(self, value):
self.value = value

class MyComponent(CustomComponent):
def build(self):
return ExternalClass("test")
"""
class_name = "MyComponent"
created_class = create_class(code, class_name)
instance = created_class()
result = instance.build()
assert result.value == "test"


def test_create_class_with_multiple_external_classes():
code = """
from langflow.custom import CustomComponent
Expand Down
Loading