fix(custom): honor asname in from X import Y as Z for custom components - #12813
Conversation
…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`.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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_attributesto keyexec_globalsbyalias.asname or alias.nameforast.ImportFrom. - Add regression/unit tests covering aliased
from ... import ... as ...inprepare_global_scope. - Add an end-to-end test verifying
create_classcan 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 Report✅ All modified and coverable lines are covered by tests. ❌ 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@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Adam-Aghili
left a comment
There was a problem hiding this comment.
LGTM~
Pretty basic change we now check for an alias first
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.
…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)
Summary
from pkg import Foo as Barfail withNameError: name 'Bar' is not definedand never appear in the sidebar._handle_module_attributesin src/lfx/src/lfx/custom/validate.py was keyingexec_globalsonalias.name, so the original name was bound in the component's exec scope instead of the alias.alias.asname or alias.nameas the key, matching theimport X as Ybranch a few lines above in the same file and standard Python import semantics.Reproduction (before fix)
Test plan
test_prepare_global_scope_supports_aliased_from_imports— asserts the alias (to_url_path) is bound and the original name (pathname2url) is not.test_create_class_supports_aliased_from_imports— end-to-end throughcreate_class, instantiates the component and calls the aliased function.pytest tests/unit/custom/component/test_validate.pyfromsrc/lfx— 46 passed.Notes
mainandrelease-1.10.0; happy to cherry-pick after this lands.execute_functionhas a separate, pre-existing limitation (it does not handleast.ImportFromat all), but that is out of scope for this issue.