-
Notifications
You must be signed in to change notification settings - Fork 252
fix Semantic highlighting incorrect for original name in import X as Y statements #2045 #2115
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
base: main
Are you sure you want to change the base?
Conversation
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes semantic highlighting for the original name in from X import Y as Z statements. Previously, the original name Y was incorrectly highlighted as a namespace token, while the alias Z was correctly highlighted with its proper semantic type (function, class, etc.). The fix ensures both names receive the same semantic highlighting based on the resolved symbol kind.
Changes:
- Removed the hardcoded namespace token assignment for original names in aliased imports from
process_ast - Added a new
add_import_from_alias_tokensfunction that properly resolves and applies semantic tokens for original import names - Refactored
process_keyto extract common logic into a newpush_symbol_rangehelper method
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pyrefly/lib/state/semantic_tokens.rs | Removed hardcoded namespace token handling for aliased imports and refactored token generation into reusable methods |
| pyrefly/lib/lsp/wasm/semantic_tokens.rs | Added logic to collect symbol kinds and apply them to original import names in aliased imports, with recursive traversal for nested statements |
| pyrefly/lib/test/lsp/semantic_tokens.rs | Updated test expectations to verify that original import names receive function tokens instead of namespace tokens |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| } | ||
| } | ||
| stmt.recurse(&mut |inner| add_import_from_alias_tokens(builder, inner, symbol_kinds)); |
Copilot
AI
Jan 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The recursive call to add_import_from_alias_tokens on line 131 is unnecessary and potentially problematic. Python ImportFrom statements cannot be nested inside other statements - they only appear at the module level or within conditional blocks. The recursion could cause the function to be called multiple times on the same import statement if it appears within nested control flow structures (like if/else blocks), leading to duplicate token generation. Since ImportFrom statements are already being processed from ast.body (lines 104-106), the recursion should be removed.
| stmt.recurse(&mut |inner| add_import_from_alias_tokens(builder, inner, symbol_kinds)); |
|
@grievejia has imported this pull request. If you are a Meta employee, you can view this in D90887939. |
Summary
Fixes #2045
Updated semantic token generation so the original name in from X import Y as Z inherits the resolved symbol kind (class/function/etc.) instead of being treated as a generic namespace, which matches the expected behavior described in issue #2045 (original name should match alias)
Test Plan
update expectations so the original imported name is typed as function in aliased imports.