ruff icon indicating copy to clipboard operation
ruff copied to clipboard

add force-alphabetical-sort-within-sections flag to isort settings

Open alexdauenhauer opened this issue 2 years ago • 17 comments

~While I personally like isort better, my company uses reorder-python-imports so I am wondering if there is already support for this and I missed it in the docs, or if support could be added? I am loving this package and would love to have it auto-run the import sorting in the format I need.~

Actually I think it is better to just add force-alphabetical-sort-within-sections flag to isort settings

alexdauenhauer avatar May 26 '23 14:05 alexdauenhauer

actually it turns out I can almost achieve the same functionality, but the isort arg that I need is force-alphabetical-sort-within-sections and ruff does not yet support this

alexdauenhauer avatar May 26 '23 15:05 alexdauenhauer

Can you share the configuration? A profile reporder-python-imports could be added.

JonathanPlasse avatar May 26 '23 16:05 JonathanPlasse

well now I think what would actually be better than adding support for reorder-python-imports, would be to just add the force-alphabetical-sort-within-sections flag for available isort args. I think isort can do everything that reorder-python-imports can do, but not all the flags for isort are available to enable that functionality

alexdauenhauer avatar May 26 '23 17:05 alexdauenhauer

@alexdauenhauer Does this solve it: https://beta.ruff.rs/docs/settings/#isort-force-sort-within-sections?

sanmai-NL avatar Jun 22 '23 14:06 sanmai-NL

@sanmai-NL unfortunately not. The sorting rules are slightly different. sort-within-sections prioritizes case over alphanumeric (e.g. MY_CONSTANT will be sorted on top of alphabetical_method)

alexdauenhauer avatar Jun 22 '23 19:06 alexdauenhauer

Any updates about this? Thanks!

perezzini avatar Aug 09 '23 15:08 perezzini

I'd like this too, I'm trying to get Ruff adopted where I work but this is one area where a lot of code gets formatted differently to our current isort configuration.

owenlamont avatar Oct 29 '23 22:10 owenlamont

Update: Actually I don't think this is an issue anymore. Once I noticed the case-sensitive argument which I set to false and I fixed and tweaked some other ruff configuration I was able to exactly reproduce my old isort configuration sorting behaviour that had force_alphabetical_sort_within_sections set to true.

owenlamont avatar Nov 01 '23 11:11 owenlamont

@owenlamont do you mind sharing your configuration to mimic force_alphabetical_sort_within_sections, please ? I didn't manage to recreate it from the description.

Pierre-Sassoulas avatar Jan 31 '24 13:01 Pierre-Sassoulas

Hi @Pierre-Sassoulas - this was my Ruff isort config taken from my pyproject.toml (note I excluded known-first-party and known-third-party modules which will be specific to your repo). This sorting was close to but not quite identical to isort. I think there were some minor differences with Ruff sorting on full relative import paths (which I personally liked) - I think the difference was something like that - I don't exactly remember but most of the time the import sort order came out identical to how we had isort configured.

[tool.ruff.isort]
case-sensitive = false
combine-as-imports = true
force-sort-within-sections = true
known-first-party = []
known-third-party = []
lines-after-imports = 2
order-by-type = false
section-order = [
    "future",
    "standard-library",
    "third-party",
    "first-party",
    "local-folder"
]

This closely matched the behaviour of the old isort config:

[tool.isort]
profile = "black"
multi_line_output = 3
force_sort_within_sections = true
force_alphabetical_sort_within_sections = true
sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]
known_first_party = []
known_thirdparty = []
skip_gitignore=true
lines_after_imports=2
combine_as_imports=true

owenlamont avatar Jan 31 '24 21:01 owenlamont

It's working, thank you a lot @owenlamont !

The sorting rules are slightly different. sort-within-sections prioritizes case over alphanumeric (e.g. MY_CONSTANT will be sorted on top of alphabetical_method)

The specific option that fix this is order-by-type = false.

I guess the issue can be closed then.

Pierre-Sassoulas avatar Feb 01 '24 10:02 Pierre-Sassoulas

I have a similar problem that I cannot seem to solve with the current settings on v0.2.1 (although I cannot find anything in the changelogs that indicate this was solved in v0.2.2 or v0.3.0).

I have the following 5 imports:

from pyspark.sql import Column, DataFrame
from pyspark.sql import functions as F
from pyspark.sql import SparkSession
from pyspark.sql import types as T

These are sorted by isort with the following config:

[tool.isort]
line_length = 120
multi_line_output = 3
force_alphabetical_sort_within_sections = "True"
force_sort_within_sections = "False"
profile = "black"

I cannot seem to get the same behaviour with ruff, as it always combines the first and third line into this:

from pyspark.sql import Column, DataFrame, SparkSession
from pyspark.sql import functions as F
from pyspark.sql import types as T

with the following config:

[tool.ruff.lint.isort]
force-sort-within-sections = false
order-by-type = false
case-sensitive = false

Am I misconfiguring anything? It seems like the alphabetical sort is sorting AFTER grouping, instead of before. And I cannot seem to turn this off.

Jari27 avatar Mar 06 '24 13:03 Jari27

@Jari27 I think you want force-single-line = true.

from pyspark.sql import Column
from pyspark.sql import DataFrame
from pyspark.sql import functions as F
from pyspark.sql import SparkSession
from pyspark.sql import types as T

samdoran avatar Apr 22 '24 20:04 samdoran

@samdoran Thanks, that is a lot closer but still not fully compatible. isort (isort==5.10.1) instead puts Column and DataFrame on the same line.

Jari27 avatar Apr 26 '24 11:04 Jari27

Yes we don't support that.

charliermarsh avatar Apr 26 '24 13:04 charliermarsh

It's intentional and documented here: https://docs.astral.sh/ruff/faq/#how-does-ruffs-import-sorting-compare-to-isort

charliermarsh avatar Apr 26 '24 13:04 charliermarsh

Ah that's fair. Not sure how I missed it! Thanks for the help!

Jari27 avatar Apr 28 '24 17:04 Jari27