pylance-release
pylance-release copied to clipboard
Unexpected, unnecessary removal of in-use though unreferenced imports in .py files nothing to do with jupyter and/or pylance
https://stackoverflow.com/questions/73266311/vscode-removes-python-imports-automatically-but-unnecessarily-on-save It removes the following in-use, important though unreferenced imports in .py file in a python project which has nothing to do with jupyter and/or pylance:
import quart.flask_patch
import os
Even after I uninstall this extension from VSCode, the process keeps running and I have to remove the folder ~/.vscode/extensions/ms-python.vscode-pylance-2022.8.10/ This sucks!
Environment data
- Language Server version: N.A
- OS and version: Ubuntu 22.04
- Python version (& distribution if applicable, e.g. Anaconda): 3.10.4
Code Snippet
import quart.flask_patch
import os
Repro Steps
- XXX
Expected behavior
XXX
Actual behavior
XXX
Logs
XXX
Could you check you settings, for codeActionsOnSave? Please see the docs: https://code.visualstudio.com/updates/v1_23#_run-code-actions-on-save
You probably need to set the following:
"source.fixAll": true,
"source.fixAll.unusedImports": false
Please let me know if this fixes it.
Yes it does. Correct config is:
{
"editor.codeActionsOnSave": {
"source.organizeImports": false,
"source.fixAll": true,
"source.fixAll.unusedImports": false
}
}
I have to turn off "source.organizeImports" as the order is important and not to be messed up by this extension!
Is there any settings chapter in https://code.visualstudio.com/docs which collect all the settings groups / sections? It doesn't make sense for this info to stay in the https://code.visualstudio.com/updates available in month/year!
Instead of having to completely turn off "source.fixAll.unusedImports", is it possible to make it so that it ignores unused imports if the line ends with # noqa? That would allow users to benefit from the feature without breaking their code.
In addition to this, I noticed that Pylance now autoformats imports to use either absolute or relative (based on "python.analysis.importFormat". There's no option to keep imports as-is.
I'm working on an open source project where there are both relative and absolute imports in the same file. I don't want to autoformat these imports because that adds unnecessary noise to my PRs. Unfortunately, there's no way to turn this off without setting "source.fixAll": false... ☹️
see this comment for more detail - https://github.com/microsoft/pylance-release/issues/3181#issuecomment-1212271684
vscode team has a tracking issue for better "editor.codeActiosOnSave": https://github.com/microsoft/vscode/issues/82718
@laymonage for this
Instead of having to completely turn off "source.fixAll.unusedImports", is it possible to make it so that it ignores unused imports if the line ends with # noqa? That would allow users to benefit from the feature without breaking their code.
can you open new issue rather than pig back on this issue? otherwise, it will be hard for us to track the request.
In addition to this, I noticed that Pylance now autoformats imports to use either absolute or relative (based on "python.analysis.importFormat". There's no option to keep imports as-is.
we do. that's basically same as turning off source.fixAll.convertImportFormat: false in editor.codeActionOnSave
Just chiming to say that that this is not really related to VS Code. Pylance decides which types of code actions are returned when source.fixAll code actions are requested. If users are confused by something being run by source.fixAll, then those source actions should not be automatically returned for source.fixAll
To fix this, one option is that Pylance tries to be smart about which actions are returned in specific calling patterns. However I think a better solution would be that Pylance surfaces more dangerous actions in their own code action categories, such as source.removeUnusedImports. That's what JS/TS in VS Code does for example. source.fixAll is intended for high-confidence, non-dangerous fixes
we moved all source.fixall.xxx to source.xxx and added new fixall named source.fixall.pylance and added new settings python.analysis.fixall which you can list source actions you want to run in source.fixall.pylance. and the option will show all available source actions.
and now you can ignore unused import using # pyright: ignore
ex)
from module import unused # pyright: ignore
then our remove all unused import won't pick unused imports on the same line. (More specifically, all diagnostics on the line will be ignored)
the change will be included in the next prerelease bit.
This issue has been fixed in prerelease version 2022.11.31, which we've just released. You can find the changelog here: CHANGELOG.md