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

Formatter does not sort inputs to match isort profile ["I"] #8612

Closed
mwip opened this issue Nov 11, 2023 · 3 comments
Closed

Formatter does not sort inputs to match isort profile ["I"] #8612

mwip opened this issue Nov 11, 2023 · 3 comments
Labels
isort Related to import sorting question Asking for support or clarification

Comments

@mwip
Copy link

mwip commented Nov 11, 2023

This could be by design, but I did not find a specific answer to the following question in the issues here. So I thought I'd ask.

Is there any option to force import sorting in ruff format in below's example? I am aware that ruff test.py --fix would solve this, but to me, this seems like an unnecessary second step. (Using a lsp integration this could mean a manual intervention when it could also be used in an on-save-hook, for example.) Am I maybe missing something in the conflicting lint rules?

Given the following MWE:

# test.py
import os
import sys
from pathlib import Path
import numpy as np
from local_module import something
# pyproject.toml
[tool.ruff]
select = [
    "I",
]

we can see ruff will check for sorted inputs:

$ ruff test.py
# test.py:1:1: I001 [*] Import block is un-sorted or un-formatted
# Found 1 error.
# [*] 1 fixable with the `--fix` option.

Yet, ruff format will not sort imports isort-style:

$ ruff format test.py
# 1 file left unchanged

This would be the expected outcome:

import os
import sys
from pathlib import Path

import numpy as np

from local_module import something
@charliermarsh
Copy link
Member

Good question -- we have an open issue to document this better (#8367).

Right now, import sorting is part of the linter (ruff check and ruff check --fix) rather than the formatter ruff format). At present, the formatter doesn't do any import sorting -- just formatting. So it won't reorder and collapse imports. This is both for historical reasons and because unlike the rest of the formatter, it requires changing the AST and so (possibly) the behavior of your code.

More practically, what this means is that import sorting is performed via ruff check and ruff check --fix rather than ruff format. With the above configuration, ruff check --fix should give you isort-like import sorting, while ruff format will handle general code formatting.

@charliermarsh charliermarsh added question Asking for support or clarification isort Related to import sorting labels Nov 11, 2023
@charliermarsh
Copy link
Member

(I'm happy to answer any follow-up questions of course, just ask :))

@mwip
Copy link
Author

mwip commented Nov 11, 2023

Thank you for the prompt and helpful reply. This totally makes sense. Excuse me missing the issue and thanks for linking it.

I will just set up my on save hooks to run ruff check --select "I" --fix for the file in addition to a ruff format. Works fine for me. (I have to make use of Emacs' customizability in some way 😉 ). Maybe this help someone else finding this in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
isort Related to import sorting question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

2 participants