-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
ruff format
doesn't sort imports, leaves file unchanged
#8926
Comments
In Ruff, import sorting and re-categorization is part of the linter, not the formatter. The formatter will re-format imports, but it won't rearrange or regroup them, because the formatter maintains the invariant that it doesn't modify the program's AST (i.e., its semantics and behavior). To get isort-like behavior, you'd want to run |
Got it, thanks! |
While the rationale makes complete sense, this does make it pretty awkward and unintuitive to sort import statements as part of auto-formatting in editor configs and such. Would it be a possibility to add a shortcut for this as a flag to
|
I agree it sounds technically correct, but a little awkward from a practical standpoint. When I need to format the code, I use a script that runs: ruff check . --select I --fix
ruff format . |
I have added [tool.ruff.lint]
extend-select = ["I"] to ruff check --fix config.py I get config.py:37:1: E402 Module level import not at top of file
Found 1 error. but nothing gets resorted in the file. |
@ruslaniv have you tried select instead of extend-select? |
Yes, just tried it [tool.ruff.lint]
select = ["I"] and in this case it's not even picking up the error: ruff check --fix config.py
All checks passed! |
@ruslaniv please consider creating a new issue, ideally with minimal repro. Can you try running |
EDIT: In my case, I just forgot to commit the I can add a similar observation to this. Having a simple file like: from pathlib import Path
import os
print(Path(os.getcwd())) and a pyproject.toml entry:
The cli command behaves as expected: ruff check temp.py
temp.py:1:1: I001 [*] Import block is un-sorted or un-formatted
Found 1 error.
[*] 1 fixable with the `--fix` option. And will correctly fix the import statement when I add However, in my pre-commit hook the file will not be fixed but accepted as is (with the wrongly ordered inputs). My pre-commit is configured as: - repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.5
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format |
@gboeer - This sounds similar to astral-sh/ruff-pre-commit#64 -- perhaps chime in there? |
This issue causes some inconvenience using the sorting functionality:
I do understand that currently import sorting and re-categorization is implemented as the linter, not the formatter, which might make sense in terms of the function group. But as a user, I think the software UI ergonomics is more important here. |
Currently ruff format does not sort imports (see astral-sh/ruff#8926). This means that executing poe format was not sorting them which can cause errors during linting check in the pipeline.
[Ruff](https://docs.astral.sh/ruff/) ([GitHub](https://github.com/astral-sh/ruff)) is a Python linter and formatter that plays the role of `black`, `isort`, `flake8`, and `pylint`, and does it all much faster (for whatever that is worth). This PR adds the **option** to use `ruff` in place of these tools to `checks-superstaq`. Specifically, this PR functionally adds: - `ruff_format_.py` (which aspires to replace one day replace `format_.py`) - `ruff_lint_.py` (which aspires to one day replace `flake8_.py` and `pylint_.py`, at which point it can be renamed to (say) `lint_.py`). - A `--ruff` flag to `checks_superstaq.all_` to use `ruff` instead of the previous formatters+linters. Note that `ruff_format_.py` has a `--fix` option instead of `--apply` for parity with the `--fix` option recognized by `ruff check` (which is called by `ruff_lint_.py`). That's because `ruff format` [refuses to change a program's AST](astral-sh/ruff#8926 (comment)), so it's not allowed to change import order. Import order therefore has to be handled by `ruff check` with `./checks/ruff_lint_.py --fix`. (Incidentally, maybe `ruff_lint_.py` should be renamed to `ruff_check_` because it calls `ruff check`? But then if+when this repo switches to using ruff it will feel silly to call `checks/check_.py`. Or maybe if+when `ruff` takes over we can have `ruff_format.py` and `ruff_check.py`?) `ruff` comes with many reasonable defaults, so I did not need to add much to `pyproject.toml`. Having said that, `checks-superstaq` has extensive configuration of `flake8` and `pylint`, and I am sure that not all of these linting rules are enforced by ruff. I leave it up to you all @Infleqtion to make the call on whether it's okay to merge in a "partial" switch to ruff (at the cost of having duplicate formatting/linting tools in the repo) vs. whether you'd like to do one big switch (configs and all). At the moment, all checks of `ruff_lint_.py` pass, and `ruff_format_.py` asks for a few minor formatting changes. The requested formatting changes are not included in this PR (though if I do make the changes, `checks/format_.py` is happy with all except those made to one `.pynb` file).
I'm looking into using ruff (0.1.6 on Python 3.12) for formatting and... it doesn't appear to be doing anything, at least in terms of replicating
isort
behaviour.Input
Command
Expected result
Or something like that.
Actual result
The file is unchanged.
What am I missing?
The text was updated successfully, but these errors were encountered: