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

Wrong sorting of imports #422

Open
hexvolt opened this issue Apr 16, 2024 · 5 comments
Open

Wrong sorting of imports #422

hexvolt opened this issue Apr 16, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@hexvolt
Copy link

hexvolt commented Apr 16, 2024

Describe the bug
The sorting of imports is not consistent with ruff CLI output.

The plugin is awesome but when it is enabled, it highlights the imports that are sorted properly by categories. If I reformat the code using the plugin, it rearranges the imports in some weird order (3rd party packages being mixed with the local code packages), which is totally different from what ruff check --select I --fix does. So if sort imports properly with the external command ruff check --select I --fix, then the plugin complains on my imports and tries to rearrange them back.

So in order to use the plugin I have to either cope with seeing yellow highlights on all (properly sorted) imports, or give up proper sorting and let plugin do the weird sorting.

To Reproduce
Steps to reproduce the behavior:

  1. Open a python file of any project, that has imports of 3rd party packages, as well as Python built-ins, and imports of local code
  2. Reformat the code using the plugin.
  3. See how the order of imports are mixed up, and the categories are NOT maintained properly (built-ins, 3rd-party, local)
  4. Now in the CLI run the official ruff command for sorting imports ruff check --select I --fix
  5. See how the imports got sorted properly.

Expected behavior
The plugin should yield the same results of imports sorting as the official ruff command ruff check --select I --fix.

Screenshots
image
You can see how imports sorted properly by the ruff check --select I --fix command are highlighted by the plugin with yellow, while it shouldn't.

image

And this is the order plugin thinks is correct but it's not.

Environments (please complete the following information):

  • IDE: PyCharm 2024.1 (Professional Edition)
  • OS: MacOS Sonoma 14.4
  • Ruff Version 0.1.15
  • Plugin version 0.0.33

Notes:

  • The plugin sees the pyproject.toml file (specified in the settings).
  • There is nothing in my pyproject.toml file about custom sorting rules or anything like that.

UPDATE
Noteworthy is that if I remove the reference to the pyproject.toml file in the plugin settings, it does sorting correctly. But in that case it ignores all other settings like line length etc.

@hexvolt hexvolt changed the title Wrong import sorting Wrong sorting of imports Apr 16, 2024
@jellehierck
Copy link

I had this issue when using ruff-lsp. Disabling that option in the plugin settings fixed this.

It seemed that somehow the lsp was not picking up (some of) the options which I set in my pyproject.toml file. I suspect the version of lsprotocol has to do with this, although I did not test this very extensively.

I had the following packages installed (managed by Poetry):

  • ruff: 0.3.7
  • ruff-lsp: 0.0.53
  • lsprotocol: 2023.0.1

Perhaps this could give a starting point for fixing this bug.

@hexvolt
Copy link
Author

hexvolt commented Apr 22, 2024

Thanks. As to my setup, ruff-lsp is not used.

@TimChild
Copy link

I'm also seeing this behaviour. @hexvolt Did you find a workaround?

@hexvolt
Copy link
Author

hexvolt commented May 28, 2024

@TimChild Nope, unfortunately I have to live with yellow warning around every import block:( To me, this is clearly a pretty major bug, idk why it is not labeled as such yet

@koxudaxi koxudaxi added the bug Something isn't working label May 30, 2024
@cm253
Copy link

cm253 commented Jun 12, 2024

Happens to me as well. Since my imports are now always out of order, I always have to use the command line as well to fix it before I commit any files :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants