microsoft / vscode-isort

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

Inconsistent sorting when running isort in terminal vs using vscode-isort #346

Closed amirkarimi closed 10 months ago

amirkarimi commented 10 months ago

Consider this file which is sorted using isort --profile black {file-name} in the terminal:

import os
from typing import Optional, cast

from flask import current_app

from app.home.models import UserContent
from app.user.models import User

And this is the same file after running vscode Organize Import command.

import os
from typing import Optional, cast

from app.home.models import UserContent
from app.user.models import User
from flask import current_app

Diff:

 import os
 from typing import Optional, cast

-from flask import current_app
-
 from app.home.models import UserContent
 from app.user.models import User
+from flask import current_app

isort log in vscode:

[info] /home/amir/my_project/backend/venv/bin/python -m isort - --profile black --filename /home/amir/my_project/file-name.py
[info] [Trace - 14:55:05] Received notification 'window/logMessage'.
[info] CWD Linter: /home/amir/my_project
[info] [Trace - 14:55:05] Received response 'codeAction/resolve - (85)' in 10ms.
[info] [Trace - 14:55:05] Sending notification 'textDocument/didSave'.
[info] [Trace - 14:55:05] Received notification 'textDocument/publishDiagnostics'.
[info] [Trace - 14:55:06] Sending request 'textDocument/codeAction - (86)'.
[info] [Trace - 14:55:06] Received response 'textDocument/codeAction - (86)' in 3ms.

My theory is that isort behaves differently when the file content is piped into it vs provided as a file.

VS Code Config

    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        },
        "editor.formatOnSave": true,
    },
    "isort.args":["--profile", "black"],

Version

OS: Ubuntu 22.04.3 Python: 3.11 isort: 5.12.0

VSCode: Commit: f1b07bd25dfad64b0167beb15359ae573aecd2cc Date: 2023-10-10T23:45:31.402Z Electron: 25.8.4 ElectronBuildId: 24154031 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Linux x64 6.2.0-36-generic snap

karthiknadig commented 10 months ago

@amirkarimi Check if you have a configuration file like setup.cfg where the order is being set. Also, this is a issue on isort library itself.

We recommend using ruff (see Ruff extension) for import sorting, as isort has not published a fix for the issues with running using stdin. We need to use stdin to handle cases where imports are organized on save.

karthiknadig commented 10 months ago

If you do install Ruff, be sure to remove isort extension, and add this to your settings ("source.organizeImports.ruff": true) in place of "source.organizeImports": true.

amirkarimi commented 10 months ago

Thanks @karthiknadig.

Check if you have a configuration file like setup.cfg where the order is being set.

The only config is in pyproject.toml which is not changing the :

[tool.isort]
profile = "black"
multi_line_output = 3

We recommend using ruff

Thanks will give it a try though we use isort and flake in our pre-commit stuff, I'd rather not change those.

I'm closing this issue since seems like nothing else can be done by the extension.

karthiknadig commented 10 months ago

@amirkarimi Ruff is a multi-tool, it implements isort, flake8, and black formatter in rust. It is incredibly fast. So you can replace two tools with one. Ruff actually implements several other linter rules too (including but not limited to pylint, pyflakes, bandit, pycodestyle, pydocstyle, etc).