Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
amirkarimi opened this issue Nov 2, 2023 · 4 comments
Closed
Labels
triage-needed Issue is not triaged.

Comments

@amirkarimi
Copy link

amirkarimi commented Nov 2, 2023

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

@github-actions github-actions bot added the triage-needed Issue is not triaged. label Nov 2, 2023
@amirkarimi amirkarimi changed the title Inconsistent sorting when running isort {file-name} and vscode isort Inconsistent sorting when running isort in terminal vs using vscode-isort Nov 2, 2023
@karthiknadig
Copy link
Member

@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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

@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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage-needed Issue is not triaged.
Projects
None yet
Development

No branches or pull requests

2 participants