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

`isort` when run via the VSCode extension is inconsistent with the result of running `isort` separately #339

Closed HarrisonWilde closed 10 months ago

HarrisonWilde commented 10 months ago

I am currently developing a package. When I use absolute imports within the package to import other parts of the package (e.g. from mypackage.module import x), isort on save from VSCode sorts these imports into the 3rd party package "section" at the top of the file. However, when I run isort explicitly on the command line on the same project, the absolute imports of parts of my package are separated from the third party ones. This is quite an annoying inconsistency as every time I commit I end up getting a warning from my pre-commit hooks.

The only config I have for isort is this in my pyproject.toml:

[tool.isort]
profile = "black"

This is the pre-commit config:

  - repo: https://github.com/pycqa/isort
    rev: 5.12.0
    hooks:
      - id: isort
        language_version: python3.11
        args: ["--settings-path", "pyproject.toml"]
HarrisonWilde commented 10 months ago

Ok, adding --known-first-party mypackage arguments to the extension settings appendage fixes this problem.

Feel free to close this issue. However, I find it odd that there would be this discrepancy as this is not required when running isort directly.

brettcannon commented 10 months ago

The discrepancy is quite possibly due to isort; we have discovered multiple places where isort operates differently when run against a file path compared to having code passed in via stdin (which is the default when using a language server).

We actually recommend people try our ruff as it supports isort-compatible import sorting, is extremely fast, and has a great VS Code extension: https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff .