Skip to content

Conversation

@asukaminato0721
Copy link
Contributor

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.

@meta-cla meta-cla bot added the cla signed label Jan 15, 2026
@github-actions
Copy link

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@asukaminato0721 asukaminato0721 marked this pull request as ready for review January 15, 2026 01:59
Copilot AI review requested due to automatic review settings January 15, 2026 01:59
Copy link

Copilot AI left a 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_tokens function that properly resolves and applies semantic tokens for original import names
  • Refactored process_key to extract common logic into a new push_symbol_range helper 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));
Copy link

Copilot AI Jan 15, 2026

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.

Suggested change
stmt.recurse(&mut |inner| add_import_from_alias_tokens(builder, inner, symbol_kinds));

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Semantic highlighting incorrect for original name in import X as Y statements

2 participants