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

Regression in Ruff's ordering of import aliases with force-sort-within-sections #8661

Closed
cmsetzer opened this issue Nov 13, 2023 · 5 comments · Fixed by #8665
Closed

Regression in Ruff's ordering of import aliases with force-sort-within-sections #8661

cmsetzer opened this issue Nov 13, 2023 · 5 comments · Fixed by #8665
Labels
bug Something isn't working isort Related to import sorting

Comments

@cmsetzer
Copy link

cmsetzer commented Nov 13, 2023

It looks like #7963 introduced a regression in Ruff's ordering of certain import aliases when force-sort-within-sections is enabled. In version 0.1.3 and prior, Ruff and isort produced identical output under this setting.

Example

  • Ruff version 0.1.5
  • isort version 5.12.0

Given pyproject.toml:

[tool.isort]
force_sort_within_sections = true

[tool.ruff.isort]
force-sort-within-sections = true

And example.py:

import encodings
from datetime import timezone as tz
from datetime import timedelta
import datetime as dt
import datetime

isort produces the following:

$ cat example.py | isort -

import datetime
import datetime as dt
from datetime import timedelta
from datetime import timezone as tz
import encodings

...while Ruff now places import datetime as dt out of order:

$ cat example.py | ruff check --fix --no-cache --select="I001" --stdin-filename="-"

import datetime
from datetime import timedelta
from datetime import timezone as tz
import datetime as dt
import encodings

Found 1 error (1 fixed, 0 remaining).
@charliermarsh
Copy link
Member

Thanks! @bluthej, are you able to take a look at this? If not, I understand and can investigate myself, but thought I would ask first since it's likely related to your recent refactor.

@charliermarsh charliermarsh added bug Something isn't working isort Related to import sorting labels Nov 13, 2023
@charliermarsh
Copy link
Member

Regardless, @cmsetzer, will make sure this is fixed for the next release. Thanks for the clear write-up.

@bluthej
Copy link
Contributor

bluthej commented Nov 13, 2023

@charliermarsh sure I'll take a look at it!

@charliermarsh
Copy link
Member

@bluthej - Thank you!

@bluthej
Copy link
Contributor

bluthej commented Nov 13, 2023

Ok so what happens is the from imports don't have an "asname", so they have a None that places them before the import ... as.

Looks like inverting the last two fields of ModuleKey fixes it, and all the tests pass, but before submitting a PR I'd like to look at the code before my refactor to make sure that this is how the ordering took place.

charliermarsh pushed a commit that referenced this issue Nov 13, 2023
Fixes #8661 

## Summary

Imports like `from x import y` don't have an "asname" for the module, so
they were placed before imports like `import x as w` since `None` <
`Some(s)` for any string s.
The fix is to first sort by `first_alias`, since it's `None` for `import
x as w`, and then by `asname`.

## Test Plan

I included the example from the issue to avoid future regressions.
charliermarsh pushed a commit that referenced this issue Nov 14, 2023
While fixing #8661 I noticed that the code structure for sorting imports
could be simplified.

## Summary

- Move the logic for `force_sort_within_sections` from `isort/mod.rs` to
`isort/ordering.rs` => now there is just one line in `isort/mod.rs`:
`let imports = order_imports(import_block, settings);` which yields the
sorted imports
- Change the function signature of `order_imports` to directly return a
`Vec<EitherImport<'a>>` => no need for `OrderedImportBlock`

I think this is a bit of an improvement because the code is simpler and
there should be a bit of a speedup when setting
`force-sort-within-sections` to true. Indeed, when it's set to true
we're now directly ordering all the imports, whereas before we would
first order the straight imports, then the from imports, combine them
and finally sort the combination a second time (this is probably not
noticeable in practice though).

## Test Plan

No tests added, this is a simple refactor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working isort Related to import sorting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants