astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.83k stars 1.1k forks source link

Support [lint.isort] group_by_package config #13389

Open an0o0nym opened 1 month ago

an0o0nym commented 1 month ago

Hi, isort linting works very nice, but I am missing a single option to finish my ruff setup - which is 'group_by_package'.

  1. Would it be easy to implement?
  2. What is my workaround for temporary solution until its implemented?
MichaReiser commented 1 month ago

Hi @an0o0nym

Thanks for the feature request. We've been hesitant to add support for more isort options. It's a non goal for us to support all isort options. But I'll keep this open to track the feature request.

an0o0nym commented 1 month ago

@MichaReiser thanks for quick reply. Is there any way I can use isort alongside ruff until its implemented [or not].?

charliermarsh commented 1 month ago

You can always use isort alongside Ruff -- just be sure not to enable Ruff's I rules.

What does this setting do exactly? It's not obvious from the docs.

an0o0nym commented 1 month ago

OK, thanks.

Ideally it should do something like :

from django.http import Http404

from rest_framework.generics import CreateAPIView
from rest_framework.generics import ListAPIView

so grouping the top-level packages into its own sections.

dhruvmanila commented 1 month ago

If I'm understanding it correctly, I think this could be achieved by sections config option to introduce the new sections and then updating the section-order although it does require additional configuration for each group of imports. For example:

["tool.ruff"]
select = [ "I" ]

["tool.ruff.isort"]
section-order = [ "future", "standard-library", "third-party", "first-party", "local-folder", "django", "rest_framework" ]
force-single-line = true

["tool.ruff.isort".sections]
django = [ "django" ]
rest_framework = [ "rest_framework" ]

The force-single-line can be used to keep the imports separated on their own line similar to your example.

Playground: https://play.ruff.rs/2eb35ec5-05b4-4432-8ac9-999b8d79882a

an0o0nym commented 3 weeks ago

@dhruvmanila sure this would work, but as you mentioned it requires naming each package manually in the config.