-
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
add force-alphabetical-sort-within-sections flag to isort settings #4670
Comments
actually it turns out I can almost achieve the same functionality, but the isort arg that I need is |
Can you share the configuration? |
well now I think what would actually be better than adding support for reorder-python-imports, would be to just add the |
@alexdauenhauer Does this solve it: https://beta.ruff.rs/docs/settings/#isort-force-sort-within-sections? |
@sanmai-NL unfortunately not. The sorting rules are slightly different. sort-within-sections prioritizes case over alphanumeric (e.g. |
Any updates about this? Thanks! |
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. |
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 do you mind sharing your configuration to mimic |
If we want to keep parity with reorder-python-imports, we need astral-sh/ruff#4670
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 |
It's working, thank you a lot @owenlamont !
The specific option that fix this is I guess the issue can be closed then. |
…e, to ruff ruff is faster and handle everything we had prior. isort configuration done based on the indication from astral-sh/ruff#4670, previousely based on reorder-python-import (pytest-dev#11896) flake8-docstrings was a wrapper around pydocstyle (now archived) that explicitly asks to use ruff in PyCQA/pydocstyle#658. flake8-typing-import is useful mainly for project that support python 3.7 and the one useful check will be implemented in astral-sh/ruff#2302 We need to keep blacken-doc because ruff does not handle detection of python code inside .md and .rst. The direct link to the repo is now used to avoid a redirection. Manual fixes: - Lines that became too long - % formatting that was not done automatically - type: ignore that were moved around - noqa of hard to fix issues (UP031 generally) - fmt: off and fmt: on that is not really identical between black and ruff - autofix re-order in pre-commit from faster to slower Co-authored-by: Ran Benita <ran@unusedvar.com>
…e, to ruff ruff is faster and handle everything we had prior. isort configuration done based on the indication from astral-sh/ruff#4670, previousely based on reorder-python-import (pytest-dev#11896) flake8-docstrings was a wrapper around pydocstyle (now archived) that explicitly asks to use ruff in PyCQA/pydocstyle#658. flake8-typing-import is useful mainly for project that support python 3.7 and the one useful check will be implemented in astral-sh/ruff#2302 We need to keep blacken-doc because ruff does not handle detection of python code inside .md and .rst. The direct link to the repo is now used to avoid a redirection. Manual fixes: - Lines that became too long - % formatting that was not done automatically - type: ignore that were moved around - noqa of hard to fix issues (UP031 generally) - fmt: off and fmt: on that is not really identical between black and ruff - autofix re-order in pre-commit from faster to slower Co-authored-by: Ran Benita <ran@unusedvar.com>
…e, to ruff (#11911) ruff is faster and handle everything we had prior. isort configuration done based on the indication from astral-sh/ruff#4670, previousely based on reorder-python-import (#11896) flake8-docstrings was a wrapper around pydocstyle (now archived) that explicitly asks to use ruff in PyCQA/pydocstyle#658. flake8-typing-import is useful mainly for project that support python 3.7 and the one useful check will be implemented in astral-sh/ruff#2302 We need to keep blacken-doc because ruff does not handle detection of python code inside .md and .rst. The direct link to the repo is now used to avoid a redirection. Manual fixes: - Lines that became too long - % formatting that was not done automatically - type: ignore that were moved around - noqa of hard to fix issues (UP031 generally) - fmt: off and fmt: on that is not really identical between black and ruff - autofix re-order in pre-commit from faster to slower Co-authored-by: Ran Benita <ran@unusedvar.com>
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 I think you want
|
@samdoran Thanks, that is a lot closer but still not fully compatible. isort ( |
Yes we don't support that. |
It's intentional and documented here: https://docs.astral.sh/ruff/faq/#how-does-ruffs-import-sorting-compare-to-isort |
Ah that's fair. Not sure how I missed it! Thanks for the help! |
While I personally likeisort
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 settingsThe text was updated successfully, but these errors were encountered: