ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Glob pattern for isort `sections` or `force-to-top`

Open serjflint opened this issue 1 year ago • 5 comments

Hi!

ruff 0.3.4

I am trying to sort imports like ruff check package/conftest.py --fix-only

import pytest

import package.pytest_init  # noqa: F401

from package import b
from package import a

I have a reason why "pytest_init" should be imported before other first-party imports. And I have many such packages.

If I sort as-is I get

import pytest

from package import a
from package import b
import package.pytest_init  # noqa: F401

Which breaks some things for me.

The solution I found is to use # isort: skip

import pytest

import package.pytest_init  # noqa: F401  # isort: skip

from package.sub import a
from package.sub import b

or to add

known-third-party = ["*.pytest_init"]
import package.pytest_init  # noqa: F401
import pytest

from package import a
from package import b

Which keeps my code working, but puts pytest_init in an awkward place.

I wish to put "*.pytest_init" in its own section or at least at the top of first-party. Is it possible to add glob patterns to those parameters?

P.S.: I am not sure if forced-separate will work for my case.

serjflint avatar Apr 14 '24 14:04 serjflint

Have you tried section-order with sections?

MichaReiser avatar Apr 15 '24 06:04 MichaReiser

Have you tried section-order with sections?

Hi! Yes, I use it.

The problem is that package is already mentioned in first-party. But I need package.pytest_init to be in a different section or at the top of first-party.

I could write all such package.pytest_init manually, but with hundreds of them it would help to have a glob pattern.

serjflint avatar Apr 15 '24 09:04 serjflint

These settings are only applied to the first segment of the import -- we don't classify beyond the top-level of the import anyway. So I don't think globs would map to the model here.

charliermarsh avatar Apr 18 '24 00:04 charliermarsh

These settings are only applied to the first segment of the import -- we don't classify beyond the top-level of the import anyway. So I don't think globs would map to the model here.

It works for known-third-party. Is it possible to spread that behavior?

It is a feature request. What should I do to be considered as such? It seems to me that you explain the current behavior.

serjflint avatar Apr 18 '24 06:04 serjflint

Right now I use pathlib and tomlkit to emulate this feature by generating pyproject.toml programmatically. And pyproject.toml is several thousand lines long.

serjflint avatar Apr 18 '24 07:04 serjflint

Right now I use pathlib and tomlkit to emulate this feature by generating pyproject.toml programmatically. And pyproject.toml is several thousand lines long.

Hi! Listing all packages manually as a first-party, a third-party or a custom one helps with my issues. But it requires adding all of them with scripts. Is possible to add patterns like 'src/*' to isort sections?

serjflint avatar Aug 08 '24 17:08 serjflint