Skip to content

fix(custom): honor asname in from X import Y as Z for custom components - #12813

Merged
erichare merged 2 commits into
release-1.9.1from
claude/fix-custom-import-alias-1-9-1
Apr 21, 2026
Merged

fix(custom): honor asname in from X import Y as Z for custom components#12813
erichare merged 2 commits into
release-1.9.1from
claude/fix-custom-import-alias-1-9-1

Conversation

@erichare

Copy link
Copy Markdown
Member

Summary

  • Custom components using from pkg import Foo as Bar fail with NameError: name 'Bar' is not defined and never appear in the sidebar.
  • _handle_module_attributes in src/lfx/src/lfx/custom/validate.py was keying exec_globals on alias.name, so the original name was bound in the component's exec scope instead of the alias.
  • Use alias.asname or alias.name as the key, matching the import X as Y branch a few lines above in the same file and standard Python import semantics.

Reproduction (before fix)

from urllib.request import pathname2url as to_url_path
from lfx.custom import Component

class MyComponent(Component):
    def to_url(self, path):
        return to_url_path(path)  # NameError: name 'to_url_path' is not defined

Test plan

  • Added test_prepare_global_scope_supports_aliased_from_imports — asserts the alias (to_url_path) is bound and the original name (pathname2url) is not.
  • Added test_create_class_supports_aliased_from_imports — end-to-end through create_class, instantiates the component and calls the aliased function.
  • pytest tests/unit/custom/component/test_validate.py from src/lfx — 46 passed.
  • Reviewer: confirm CI is green on the full lfx test suite.

Notes

  • The same bug is also present on main and release-1.10.0; happy to cherry-pick after this lands.
  • execute_function has a separate, pre-existing limitation (it does not handle ast.ImportFrom at all), but that is out of scope for this issue.

…ents

`_handle_module_attributes` was keying `exec_globals` on `alias.name`, so
`from pkg import Foo as Bar` bound `Foo` instead of `Bar` in a custom
component's exec scope. Any reference to `Bar` then raised NameError and
the component failed to load.

Use `alias.asname or alias.name`, matching the `import X as Y` branch in
the same file and standard Python import semantics. Adds regression tests
against `prepare_global_scope` and end-to-end through `create_class`.
@coderabbitai

coderabbitai Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

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.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 082f7a3c-1dda-4517-a9e2-aaabd5a7638e

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

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/fix-custom-import-alias-1-9-1

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes custom component execution scope so from X import Y as Z correctly binds Z (the alias) rather than Y, preventing NameError and allowing aliased imports to work in custom components.

Changes:

  • Update _handle_module_attributes to key exec_globals by alias.asname or alias.name for ast.ImportFrom.
  • Add regression/unit tests covering aliased from ... import ... as ... in prepare_global_scope.
  • Add an end-to-end test verifying create_class can execute code that relies on the aliased import.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/lfx/src/lfx/custom/validate.py Fixes binding behavior for from X import Y as Z when building the exec scope for custom code.
src/lfx/tests/unit/custom/component/test_validate.py Adds targeted regression + end-to-end tests to ensure aliased ImportFrom bindings work as expected.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented Apr 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.88%. Comparing base (9f42c9e) to head (76be02d).
⚠️ Report is 1 commits behind head on release-1.9.1.

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

Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff                @@
##           release-1.9.1   #12813     +/-   ##
================================================
  Coverage          52.87%   52.88%             
================================================
  Files               2020     2020             
  Lines             183735   183736      +1     
  Branches           27355    28849   +1494     
================================================
+ Hits               97159    97164      +5     
+ Misses             85477    85474      -3     
+ Partials            1099     1098      -1     
Flag Coverage Δ
backend 55.81% <ø> (+0.01%) ⬆️
frontend 52.89% <ø> (-0.01%) ⬇️
lfx 49.99% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/lfx/src/lfx/custom/validate.py 57.53% <100.00%> (+0.12%) ⬆️

... and 22 files 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.

@Adam-Aghili Adam-Aghili left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM~
Pretty basic change we now check for an alias first

@github-actions github-actions Bot added the lgtm This PR has been approved by a maintainer label Apr 21, 2026
fix: Make sdk env flag idempotent

Refactor pytest_addoption to register CLI options via a loop with
contextlib.suppress(ValueError), allowing langflow-sdk and lfx to
coexist without plugin registration conflicts.

Add test_pytest_addoption_is_idempotent to verify behavior.
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Apr 21, 2026
@erichare erichare added lgtm This PR has been approved by a maintainer and removed lgtm This PR has been approved by a maintainer labels Apr 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Frontend Unit Test Coverage Report

Coverage Summary

Lines Statements Branches Functions
Coverage: 34%
34.96% (40044/114539) 68.02% (5541/8146) 35.82% (934/2607)

Unit Test Results

Tests Skipped Failures Errors Time
3980 0 💤 0 ❌ 0 🔥 7m 3s ⏱️

@erichare
erichare added this pull request to the merge queue Apr 21, 2026
Merged via the queue into release-1.9.1 with commit cae2bdf Apr 21, 2026
114 of 115 checks passed
@erichare
erichare deleted the claude/fix-custom-import-alias-1-9-1 branch April 21, 2026 16:05
erichare added a commit that referenced this pull request Apr 24, 2026
…ents (#12813)

* fix(custom): honor asname in `from X import Y as Z` for custom components

`_handle_module_attributes` was keying `exec_globals` on `alias.name`, so
`from pkg import Foo as Bar` bound `Foo` instead of `Bar` in a custom
component's exec scope. Any reference to `Bar` then raised NameError and
the component failed to load.

Use `alias.asname or alias.name`, matching the `import X as Y` branch in
the same file and standard Python import semantics. Adds regression tests
against `prepare_global_scope` and end-to-end through `create_class`.

* fix: Make sdk env flag idempotent (release-1.9.1) (#12815)

fix: Make sdk env flag idempotent

Refactor pytest_addoption to register CLI options via a loop with
contextlib.suppress(ValueError), allowing langflow-sdk and lfx to
coexist without plugin registration conflicts.

Add test_pytest_addoption_is_idempotent to verify behavior.

(cherry picked from commit cae2bdf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: from X import Y as Z alias ignored in custom component code — NameError on aliased imports

3 participants