Skip to content

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

@PyPippo

Description

@PyPippo

Describe the Bug

Description

When using import X as Y syntax, Pyrefly correctly highlights the alias (Y) but incorrectly highlights the original name (X) as an unknown/generic object instead of preserving its semantic type (class, function, etc.).

Steps to Reproduce

  1. Create two Python files as shown below
  2. Open test_imports.py in VSCode with Pyrefly enabled
  3. Use "Developer: Inspect Editor Tokens and Scopes" on the statement from my_module import MyClass as MC
  4. Inspect the token for MyClass (the original name before as)

File 1: my_module.py

class MyClass:
    """A sample class."""
    def __init__(self, value: int):
        self.value = value

def my_function() -> str:
    return "Hello"

MY_CONSTANT = 42

File 2: test_imports.py

from my_module import MyClass
from my_module import MyClass as MC
from my_module import my_function as mf

Expected Behavior

Both MyClass and MC should receive the same semantic highlighting (class type), similar to how Pylance handles this case.

In the import statement from my_module import MyClass as MC:

  • MyClass should be highlighted as a class
  • MC should be highlighted as a class

Actual Behavior

  • MC is correctly highlighted as a class ✓
  • MyClass is highlighted as an unknown/generic object ✗

When inspecting tokens:

  • MC shows correct semantic token type
  • MyClass shows generic/unknown semantic token type

Environment

  • VSCode version: [inserisci la tua versione]
  • Pyrefly version: [inserisci la tua versione]
  • OS: [il tuo sistema operativo]

Comparison with Pylance

Pylance correctly highlights both the original name and the alias with their appropriate semantic types.

Additional Context

This issue affects code readability, especially in __init__.py files where the import X as X pattern is commonly used for explicit re-exports and type checking compatibility.

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

VSCode version: 1.107.1
Pyrefly version: 0.46.3
OS: Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedLarger than "good first issue", but still well-defined and ready for someone to pick uplanguage-serverIssues specific to our IDE integration rather than type checking

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions