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

isort and ruff behavior mismatch w.r.t. relative imports and relative-imports-order option. #10655

Closed
Autopilot9369 opened this issue Mar 29, 2024 · 2 comments · Fixed by #10669
Assignees
Labels
bug Something isn't working isort Related to import sorting

Comments

@Autopilot9369
Copy link

Autopilot9369 commented Mar 29, 2024

I have the following code:

# view.py

from .utils import create_question
from django_polls.apps.polls.models import Choice
from .models import Question
from ..models import ABC

and the following ruff config:

[tool.ruff.lint.isort]
case-sensitive = false
combine-as-imports = true
force-wrap-aliases = true
split-on-trailing-comma = true
default-section = "third-party"
detect-same-package = false
force-single-line = false
force-sort-within-sections = false
from-first = false
lines-between-types = 0
lines-after-imports = 2
extra-standard-library = ["typing_extensions"]
known-first-party = []
known-local-folder = ["django_polls"]
no-lines-before = ["future", "standard-library"]
order-by-type = false
relative-imports-order = "closest-to-furthest"
section-order = [
    "future",
    "standard-library",
    "third-party",
    "first-party",
    "local-folder",
]

now if I run ruff check . --select I --fix, the fixed code is as follows:

from django_polls.apps.polls.models import Choice
from .models import Question
from .utils import create_question
from ..models import ABC

but with the following isort config:

[tool.isort]
force_alphabetical_sort_within_sections = true
force_sort_within_sections = false
from_first = false
known_first_party = []
known_local_folder = ["django_polls"]
known_tests = ["tests"]
#line_length = 88
lines_after_imports = 2
lines_between_sections = 1
profile = "black"
reverse_relative = true
sections = [
    "FUTURE",
    "STDLIB",
    "THIRDPARTY",
    "FIRSTPARTY",
    "LOCALFOLDER",
]
src_paths = ["src", "tests"]

I get the following:

from .models import Question
from .utils import create_question
from ..models import ABC
from django_polls.apps.polls.models import Choice

I would prefer the output by isort, but I want to understand the behavior mismatch. I would assume that ruff's relative-imports-order = "closest-to-furthest" is equivalent to isort's reverse_relative = true. Please help me out.
Why is the local folder (django_polls) imports treated as closest rather than relative . and .. imports?

@charliermarsh charliermarsh self-assigned this Mar 30, 2024
@charliermarsh charliermarsh added the isort Related to import sorting label Mar 30, 2024
@charliermarsh
Copy link
Member

We probably just never tested because the "local folder" category typically only include relative imports. I would say our behavior is not quite correct, since the relative-imports-order should only affect relative imports, and not other imports in the "local folder" category.

@charliermarsh charliermarsh added the bug Something isn't working label Mar 30, 2024
charliermarsh added a commit that referenced this issue Mar 30, 2024
## Summary

When `relative-imports-order = "closest-to-furthest"` is set, we should
_still_ put non-relative imports after relative imports. It's rare for
them to be in the same section, but _possible_ if you use
`known-local-folder`.

Closes #10655.

## Test Plan

New tests.

Also sorted this file:

```python
from ..models import ABC
from .models import Question
from .utils import create_question
from django_polls.apps.polls.models import Choice
```

With both:

- `isort view.py`
- `ruff check view.py --select I --fix`

And the following `pyproject.toml`:

```toml
[tool.ruff.lint.isort]
order-by-type = false
relative-imports-order = "closest-to-furthest"
known-local-folder = ["django_polls"]

[tool.isort]
profile = "black"
reverse_relative = true
known_local_folder = ["django_polls"]
```

I verified that Ruff and isort gave the same result, and that they
_still_ gave the same result when removing the relevant setting:

```toml
[tool.ruff.lint.isort]
order-by-type = false
known-local-folder = ["django_polls"]

[tool.isort]
profile = "black"
known_local_folder = ["django_polls"]
```
@charliermarsh
Copy link
Member

Fixed in the next release.

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.

2 participants