microsoft / vscode-isort

Import sorting for python using the isort library.
https://marketplace.visualstudio.com/items?itemName=ms-python.isort
MIT License
83 stars 19 forks source link

Python organize imports explicit is deleting one extra line that it should when removing blank lines after an import statement. #402

Closed jose-tim closed 3 months ago

jose-tim commented 4 months ago

Type: Bug

Let say this is my code:

from fastapi import FastAPI

app = FastAPI(debug=True)

Behaviour

When i save my file, having the format on save TURNED OFF, the isort or vscode decides to remove one of the blank lines between the from statement and the app var assignation... but it also removes de variable assignation.

Expected vs. Actual

Actual code result:

from fastapi import FastAPI

(yep, two blank lines)

Expected code result:

from fastapi import FastAPI

app = FastAPI(debug=True)

Steps to reproduce:

Install isort extension (not preview). Create a settings.json file on the folder .vscode with:

{
  "[python]": {
    "editor.codeActionsOnSave": {
      "source.organizeImports": "explicit"
    }
  }
}

Create a main.py with the first provided code. Save the file with ctrl+s.

You will see it removes the lines 3 and 4.

Now disable the isort extension. Restore the code (to the 2 blank lines in the middle version). Save it again.

You wil see it removes only the line 3.

Diagnostic data

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

``` XXX ```

User Settings

``` languageServer: "Pylance" ```

Extension version: 2023.10.1 VS Code version: Code 1.87.2 (863d2581ecda6849923a2118d93a088b0745d9d6, 2024-03-08T15:20:17.278Z) OS version: Windows_NT x64 10.0.19045 Modes:

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz (12 x 2592)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|undefined| |Memory (System)|15.84GB (4.26GB free)| |Process Argv|. --crash-reporter-id d386ca9b-5407-4ec9-a87c-d002604ad24a| |Screen Reader|no| |VM|0%|
A/B Experiments ``` vsliv368cf:30146710 vspor879:30202332 vspor708:30202333 vspor363:30204092 vstes627:30244334 vscorecescf:30445987 vscod805cf:30301675 binariesv615:30325510 vsaa593cf:30376535 py29gd2263:30899288 c4g48928:30535728 azure-dev_surveyone:30548225 vscrp:30673768 962ge761:30959799 pythongtdpath:30769146 welcomedialog:30910333 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pyreplss1:30897532 pythonmypyd1:30879173 pythoncet0:30885854 pythontbext0:30879054 accentitlementst:30995554 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 cppperfcontrol:30979541 d34g3935:30971562 fegfb526:30981948 bg6jg535:30979843 ccp2r6:30993542 dsvsc020:30976470 pythonait:30996668 666c4381:30973075 7ghi1836:30988842 dsvsc021:30996838 ```
karthiknadig commented 4 months ago

@jose-tim It looks like you might have a formatter installed as well. The extension is actually a wrapper around isort and you can look at Output > isort to see what it does. If the code is being changed after you disable isort extension, it might be due to a formatter. If you have ruff formatter you way want to disable import formatting using isort and use ruff as the formatter as ruff implements both black and isort together.

karthiknadig commented 3 months ago

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on then we will be happy to re-open this issue to pick up where we left off.