-
Notifications
You must be signed in to change notification settings - Fork 259
Description
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
- Create two Python files as shown below
- Open
test_imports.pyin VSCode with Pyrefly enabled - Use "Developer: Inspect Editor Tokens and Scopes" on the statement
from my_module import MyClass as MC - Inspect the token for
MyClass(the original name beforeas)
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 = 42File 2: test_imports.py
from my_module import MyClass
from my_module import MyClass as MC
from my_module import my_function as mfExpected 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:
MyClassshould be highlighted as a classMCshould be highlighted as a class
Actual Behavior
MCis correctly highlighted as a class ✓MyClassis highlighted as an unknown/generic object ✗
When inspecting tokens:
MCshows correct semantic token typeMyClassshows 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