Skip to content

Implement import sorting - #633

Merged
charliermarsh merged 10 commits into
mainfrom
charlie/isort
Nov 11, 2022
Merged

charliermarsh merged 10 commits into
mainfrom
charlie/isort

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Nov 7, 2022 •

Copy link
Copy Markdown
Member

This PR implements isort-like import sorting behavior. The intent is to mirror isort when used with the following configuration:

[tool.isort]
profile = "black"
combine_as_imports = true

The main limitations in the current approach are as follows:

  1. We still have no way of reconciling conflicting autofix errors. So if you have unused and unsorted imports, Ruff will only fix one of the two in the first pass (probably the unsorted imports, since that block comes first), and you'll have to run Ruff again to fix the other.
  2. We don't have any comment-handling, so all comments are removed entirely

For now, this is implemented via an entirely separator visitor, mostly to make the code easier to reason about. I need to benchmark the impact of that choice.

Resolves: #465.

@charliermarsh
charliermarsh force-pushed the charlie/isort branch 8 times, most recently from 0d68d43 to 6de2898 Compare November 10, 2022 18:52
@charliermarsh
charliermarsh marked this pull request as ready for review November 10, 2022 23:48
@nik-hil

nik-hil commented Jun 12, 2024 •

Copy link
Copy Markdown

If somebody is wondering how to add this in pre-commit, use this

-   repo: https://github.com/charliermarsh/ruff-pre-commit
    rev: v0.2.0
    hooks:
    -   id: ruff
        args: ["check", "--select", "I", "--fix"]
    -   id: ruff-format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import-sorting (a ruff approximation of isort)

2 participants