ruff
ruff copied to clipboard
Glob pattern for isort `sections` or `force-to-top`
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.
Have you tried section-order with sections?
Have you tried
section-orderwithsections?
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.
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.
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.
Right now I use pathlib and tomlkit to emulate this feature by generating pyproject.toml programmatically. And pyproject.toml is several thousand lines long.
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?