Repository navigation
Different import sorting order between isort and ruff if import is aliased. #1381
Description
Activity
- addedisortRelated to Ruff import sortingRelated to Ruff import sorting
on Dec 26, 2022 Good call, thank you.
- changed the title
[-]Differnt import sorting order between isort and ruff if import is aliased.[/-][+]Different import sorting order between isort and ruff if import is aliased.[/+]on Dec 26, 2022 Related: PyCQA/isort#1952
Reacted by Gert HulselmansPersonally I like the ruff behavior more than the isort behavior. So I hope isort changes their behavior.
Reacted by Jad Haddad, Arseny Boykov, Harshil and Erik BjäreholtI get a completely different sorting when using
isortandruff. In my project,isort file.pygives me:from abc import abstractmethod from contextlib import contextmanager from numpy import concatenate, int32, zeros from traits.api import Dict, Float, Str, cached_property import tapy.core.config as config from tapy.core.base import BasicObject
whereas
ruff --select I001 --ignore F401 --fix file.pygives mefrom abc import abstractmethod from contextlib import contextmanager import tapy.core.config as config from numpy import concatenate, int32, zeros from tapy.core.base import BasicObject from traits.api import Dict, Float, Str, cached_property
Any idea what is going on here? I don't think it is an issue with my config.
It looks like it’s not registering tapy as a first-party module. Would you mind posting your pyproject.toml and project structure?
It looks like it’s not registering tapy as a first-party module. Would you mind posting your pyproject.toml and project structure?
Hi. Thanks for the swift reply! Sure thing, I have a
srcstructure, so submodules are contained insrc/tapy/submodulewith__init__.pyintapyandsubmoduledirectories. The relevant part of mypyproject.tomlis[project] name = "tapy" ... requires-python = ">=3.9" ... dependencies = [ "tqdm", "traits" ] [project.optional-dependencies] dev = [ "ipython", "pre-commit", "ruff", "sphinx", "sphinx-autoapi", "nbsphinx", "sphinx-rtd-theme", "sphinx-gallery" ] [build-system] requires = ["hatchling"] build-backend = "hatchling.build" [tool.ruff] ignore = [ "D100", "D101", "D102", "D103", "D104", "D105", "D107", "D203", "D213", "B905", "F401", "N806"] line-length = 120 select = ["B", "D", "E", "F", "I", "N", "Q", "W"] [tool.ruff.flake8-quotes] inline-quotes = "single" [tool.ruff.pydocstyle] convention = "numpy"
Can you try adding the following?
[tool.ruff] src = ["src"]
Reacted by Nathan McDougall , kwongmin Lim, Markus Legner, Scott Staniewicz, 盐粒 Yanli, Biel Stela, Jon Grace-Cox, Justus Kilian Wolff and Braden KelleyReacted by jeffcwlai(By default, we use
src = ["."]which wouldn't pick up your modules.)OK, so I just found the section in the readme regarding
known-first-partymodules withtool.ruff.isortwhich I have not set. However, now everything works without settingsrcnorknown-first-partywhich is very peculiar.Thanks for helping out. I don't want to hijack this issue. If I encounter this problem again, I will give an update. For now, it seems to 'just work again'...
(Is it possible you were missing an
__init__.pysomewhere? Feel free to file another issue if you have any follow-ups :))Reacted by Art PellingYes, exactly I just found it out, too. I had an
__init__.pyinsrcthat produces that behaviour without settingsrc.So either no
__init__.pyinsrcor settingsrc = ["src"]works correctly. Thanks a lot. Sorry for the clutter, I thought this was related due to the import alias.No prob at all, glad it’s resolved!
I added a note on this to the docs. I think I view this as a known, but acceptable deviation for now.
- added a commit that references this issue
on Jan 14, 2025
Differnt import sorting order between isort and ruff if import is aliased.