pylance-release
pylance-release copied to clipboard
"Cannot find a symbol to move" when moving symbol from/to filename with leading digit
Environment data
- Language Server version: 2024.3.100
Code Snippet
1.py
CONSTANT = 1
Repro Steps
- In a new directory, create
1.pyas shown above. - Create empty file
2.py - In
1.py, right-click onCONSTANT> Refactor... > Move symbol to ... - Select
2.pyas the destination.
Expected behavior
Either CONSTANT is moved to 2.py or the user is provided with an explanation of why it can't be moved.
Actual behavior
...which obviously doesn't help the user.
The underlying issue is that "1" and "2" are not valid module names. Digits are allowed in identifier names, but not as the first character. See https://docs.python.org/3.11/reference/lexical_analysis.html#identifiers.
For move to succeed, both the source and target filenames must be legal module names. However, I believe that's technically only required if the move results in src->dest or dest->src imports being added. In this simple scenario, CONSTANT = 1 could have been successfully moved because no imports are needed.
I found this extremely confusing and only figured out why the move wasn't allowed by debugging Pylance. So while the simplest fix would probably be to not show the Move Symbol refactorings in this scenario, that would be equally confusing IMO. This is another argument for addressing https://github.com/microsoft/pylance-release/issues/5551 in my mind.
In this simple scenario, CONSTANT = 1 could have been successfully moved because no imports are needed.
problem is this is expensive to figure out. so not something we would do in code action provider since it will require find all references
so, I think practical mitigation might be showing better message? either in message box after execution or in code action menu