Skip to content

feat: add dependencies, improves error handling in global scope and adds tests#9865

Merged
ogabrielluiz merged 9 commits intomainfrom
test-starterprojects-lfx
Sep 15, 2025
Merged

feat: add dependencies, improves error handling in global scope and adds tests#9865
ogabrielluiz merged 9 commits intomainfrom
test-starterprojects-lfx

Conversation

@ogabrielluiz
Copy link
Contributor

@ogabrielluiz ogabrielluiz commented Sep 15, 2025

Improve error handling in the prepare_global_scope function to provide clearer error messages. Add comprehensive tests to ensure all starter project templates can be loaded without import errors. These tests validate the existence of starter projects and check for proper JSON formatting.

Summary by CodeRabbit

  • New Features

    • None.
  • Bug Fixes

    • Improved error reporting for missing modules by preserving original ModuleNotFoundError details, providing clearer guidance on what’s missing.
  • Tests

    • Added comprehensive CLI tests covering all starter project templates to ensure they parse and run without import issues.
    • Verified execution via stdin and inline JSON, plus various CLI options, to improve reliability and catch regressions.
  • Chores

    • Updated comments to reflect the refined error propagation behavior.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 15, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title Check ✅ Passed The title references the main changes—improving global-scope error handling and adding tests—so it is related to the changeset; however it also claims "add dependencies", which the provided summaries do not show and is therefore misleading, and the phrasing is slightly wordy.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added the refactor Maintenance tasks and housekeeping label Sep 15, 2025
@ogabrielluiz ogabrielluiz changed the title refactor: update error handling in prepare_global_scope and add tests for starter projects feat: addd langchain dependency, improves error handling in global scope and adds tests Sep 15, 2025
@github-actions github-actions bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Sep 15, 2025
@ogabrielluiz ogabrielluiz changed the title feat: addd langchain dependency, improves error handling in global scope and adds tests feat: add langchain dependency, improves error handling in global scope and adds tests Sep 15, 2025
@github-actions github-actions bot added enhancement New feature or request and removed refactor Maintenance tasks and housekeeping enhancement New feature or request labels Sep 15, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (5)
src/lfx/src/lfx/custom/validate.py (3)

350-354: Use _import_module_with_warnings for consistency and to suppress known deprecation noise.

Direct importlib.import_module here bypasses your _import_module_with_warnings logic (used below for ImportFrom), so import langchain... via plain imports may emit avoidable warnings and behave inconsistently with from ... import ....

-            # Let importlib.import_module raise its own ModuleNotFoundError with the actual missing module
-            exec_globals[variable_name] = importlib.import_module(module_name)
+            # Let import errors propagate, but keep warning behavior consistent with ImportFrom
+            exec_globals[variable_name] = _import_module_with_warnings(module_name)

379-384: Raising the “last” error can mislead when trying langflow→lfx fallbacks. Provide aggregated context and chain the cause.

If both imports fail, the last attempt will be lfx.*, masking that user code imported langflow.*. Emit an error listing all attempts and chain the original cause for debuggability.

-        # Re-raise the last error to preserve the actual missing module information
-        if last_error:
-            raise last_error
-        msg = f"Module {node.module} not found. Please install it and try again"
-        raise ModuleNotFoundError(msg)
+        # Provide context of all attempts and preserve original traceback as cause
+        if last_error:
+            attempts = ", ".join(module_names_to_try)
+            raise ModuleNotFoundError(
+                f"Unable to import from any of: {attempts}. Last error: {last_error}"
+            ) from last_error
+        raise ModuleNotFoundError(f"Module {node.module} not found. Please install it and try again.")

311-321: Align ImportFrom attribute errors with Python semantics (“cannot import name ...”).

When a base module exists but an attribute doesn’t, current logic escalates to ModuleNotFoundError (for module.attr) instead of the idiomatic ImportError: cannot import name 'attr' from 'module'. This makes diagnostics noisier and can trip consumers relying on error type.

 def _handle_module_attributes(imported_module, node, module_name, exec_globals):
     """Handle importing specific attributes from a module."""
     for alias in node.names:
         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"{module_name}.{alias.name}"
-            exec_globals[alias.name] = importlib.import_module(full_module_path)
+            # If that fails, try importing a submodule with the same name.
+            full_module_path = f"{module_name}.{alias.name}"
+            try:
+                exec_globals[alias.name] = importlib.import_module(full_module_path)
+            except ModuleNotFoundError as e:
+                # Match standard Python error semantics for missing attribute imports.
+                raise ImportError(f"cannot import name '{alias.name}' from '{module_name}'") from e
src/lfx/tests/unit/cli/test_run_starter_projects.py (2)

16-16: Stabilize CLI tests by forcing a local SQLite DB.

Prevents accidental coupling to dev/prod DB config and mirrors the backend test fixture pattern.

-runner = CliRunner()
+runner = CliRunner(env={"LANGFLOW_DATABASE_URL": "sqlite:///:memory:"})

57-70: Optional: make stderr available explicitly for richer assertions.

If you need to inspect stderr separately (color, tracebacks), call runner.invoke(..., mix_stderr=False) in each invoke and then use result.stderr. Not required if using result.output.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 974cbe0 and 363d60f.

📒 Files selected for processing (2)
  • src/lfx/src/lfx/custom/validate.py (2 hunks)
  • src/lfx/tests/unit/cli/test_run_starter_projects.py (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-07-21T14:16:14.125Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.125Z
Learning: Applies to src/backend/tests/**/*.py : Test flows using predefined JSON data and utility functions such as 'create_flow', 'build_flow', 'get_build_events', and 'consume_and_assert_stream' in backend Python tests.

Applied to files:

  • src/lfx/tests/unit/cli/test_run_starter_projects.py
🧬 Code graph analysis (2)
src/lfx/tests/unit/cli/test_run_starter_projects.py (1)
src/backend/tests/conftest.py (1)
  • runner (437-439)
src/lfx/src/lfx/custom/validate.py (1)
src/backend/base/langflow/interface/importing/utils.py (1)
  • import_module (7-32)

@ogabrielluiz ogabrielluiz added the lgtm This PR has been approved by a maintainer label Sep 15, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Sep 15, 2025
@codecov
Copy link

codecov bot commented Sep 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 21.47%. Comparing base (061449b) to head (f28c1f0).
⚠️ Report is 16 commits behind head on main.

❌ Your project status has failed because the head coverage (46.49%) is below the target coverage (55.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #9865   +/-   ##
=======================================
  Coverage   21.47%   21.47%           
=======================================
  Files        1074     1074           
  Lines       39650    39650           
  Branches     5418     5418           
=======================================
+ Hits         8513     8516    +3     
+ Misses      30993    30990    -3     
  Partials      144      144           
Flag Coverage Δ
backend 46.49% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Sep 15, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Sep 15, 2025
@ogabrielluiz ogabrielluiz changed the title feat: add langchain dependency, improves error handling in global scope and adds tests feat: add dependencies, improves error handling in global scope and adds tests Sep 15, 2025
@github-actions github-actions bot removed the enhancement New feature or request label Sep 15, 2025
@github-actions github-actions bot added the enhancement New feature or request label Sep 15, 2025
@github-actions github-actions bot added enhancement New feature or request and removed enhancement New feature or request labels Sep 15, 2025
@sonarqubecloud
Copy link

"asyncer>=0.0.8",
"structlog",
"loguru>=0.7.3",
"langchain>=0.3.23",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this safe?

@ogabrielluiz ogabrielluiz merged commit e361b94 into main Sep 15, 2025
29 of 32 checks passed
@ogabrielluiz ogabrielluiz deleted the test-starterprojects-lfx branch September 15, 2025 19:01
lucaseduoli pushed a commit that referenced this pull request Sep 16, 2025
…dds tests (#9865)

Co-authored-by: Edwin Jose <edwin.jose@datastax.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants