microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.32k stars 1.18k forks source link

`source.organizeImports` doesn't remove unused imports #17463

Closed DetachHead closed 1 year ago

DetachHead commented 3 years ago

source.organizeImports does not remove unused imports

Environment data

[NOTE: If you suspect that your issue is related to the Microsoft Python Language Server (python.languageServer: 'Microsoft'), please download our new language server Pylance from the VS Code marketplace to see if that fixes your issue]

Expected behaviour

unused imports are removed, according to https://github.com/microsoft/vscode/issues/107535 it's supposed to

Actual behaviour

unused imports are not removed

Steps to reproduce:

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

  1. import some unused things (in non-alphabetical order so you know it's running source.organizeImports)
    from typing import List, Dict
  2. save the file

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

``` XXX ```

karthiknadig commented 3 years ago

Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 5 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

DetachHead commented 3 years ago

i think this is more of a bug than a feature request, because it's not consistent with how the same option works in typescript

karthiknadig commented 3 years ago

Sorting in python is provided by isort library. This would essentially have to be supported by isort first, and then we can work with it to provide this. Since there is no implementation of this in the extension, we are treating this as a feature.

brettcannon commented 2 years ago

Thank you to everyone who upvoted this issue! Since the community showed interest in this feature request we will leave this issue open as something to consider implementing at some point in the future.

We do encourage people to continue 👍 the first/opening comment as it helps us prioritize our work based on what the community seems to want the most.

micimize commented 2 years ago

In case others find it useful – until isort support (https://github.com/PyCQA/isort/issues/1105) I'm getting by with an autoflake as a task which I then trigger on save. It results in some "content of the file is newer" complaints but is mostly fine.

task.json#tasks entry ```json { "type": "process", "label": "autoflake.removeUnusedImports", "command": "${command:python.interpreterPath}", "args": [ "-m", "autoflake", "-i", "--remove-all-unused-imports", "${file}" ] }, ```
brettcannon commented 2 years ago

Do note that Pylance provides a code action to remove an unused import:

image

If you want something that does a larger removal of all unused imports, there's a feature request at https://github.com/microsoft/pylance-release/issues/1722 .

karrtikr commented 1 year ago

Extension now supports removing unused imports via Pylance. As isort libary does not provide this feature there's much to be done atm, hence closing this.

Please open a feature request at Pylance if you would to automatically do this on save.

micimize commented 1 year ago

Automatic unused import removal can be configured in pylance as of august: https://devblogs.microsoft.com/python/python-in-visual-studio-code-august-2022-release/#remove-all-unused-imports

Granular source.fixAll configuration can be done via source.fixAll.unusedImports though not sure how many sub-configs there are.

karrtikr commented 1 year ago

Thanks for the reference!