Skip to content
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

Different import sorting order between isort and ruff if import is aliased. #1381

Closed
ghuls opened this issue Dec 26, 2022 · 13 comments
Closed
Labels
isort Related to import sorting

Comments

@ghuls
Copy link

ghuls commented Dec 26, 2022

Differnt import sorting order between isort and ruff if import is aliased.

# isort --profile=black input.py
$ cat /tmp/isorted_output.py
from numpy import cos, int8, int16, int32, int64
from numpy import sin as np_sin
from numpy import tan, uint8, uint16, uint32, uint64

$ ruff --select I001 --ignore F401 --fix input.py
$ cat /tmp/ruff_isorted_output.py
from numpy import cos, int8, int16, int32, int64, tan, uint8, uint16, uint32, uint64
from numpy import sin as np_sin
@charliermarsh charliermarsh added the isort Related to import sorting label Dec 26, 2022
@charliermarsh
Copy link
Member

Good call, thank you.

@charliermarsh charliermarsh 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. Dec 26, 2022
@andersk
Copy link
Contributor

andersk commented Dec 27, 2022

Related: PyCQA/isort#1952

@ghuls
Copy link
Author

ghuls commented Dec 27, 2022

Personally I like the ruff behavior more than the isort behavior. So I hope isort changes their behavior.

@artpelling
Copy link

I get a completely different sorting when using isort and ruff. In my project, isort file.py gives 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.py gives me

from 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.

@charliermarsh
Copy link
Member

It looks like it’s not registering tapy as a first-party module. Would you mind posting your pyproject.toml and project structure?

@artpelling
Copy link

artpelling commented Jan 4, 2023

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 src structure, so submodules are contained in src/tapy/submodule with __init__.py in tapy and submodule directories. The relevant part of my pyproject.toml is

[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"

@charliermarsh
Copy link
Member

Can you try adding the following?

[tool.ruff]
src = ["src"]

@charliermarsh
Copy link
Member

(By default, we use src = ["."] which wouldn't pick up your modules.)

@artpelling
Copy link

artpelling commented Jan 4, 2023

OK, so I just found the section in the readme regarding known-first-party modules with tool.ruff.isort which I have not set. However, now everything works without setting src nor known-first-party which 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'...

@charliermarsh
Copy link
Member

(Is it possible you were missing an __init__.py somewhere? Feel free to file another issue if you have any follow-ups :))

@artpelling
Copy link

Yes, exactly I just found it out, too. I had an __init__.py in src that produces that behaviour without setting src.

So either no __init__.py in src or setting src = ["src"] works correctly. Thanks a lot. Sorry for the clutter, I thought this was related due to the import alias.

@charliermarsh
Copy link
Member

No prob at all, glad it’s resolved!

@charliermarsh
Copy link
Member

I added a note on this to the docs. I think I view this as a known, but acceptable deviation for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
isort Related to import sorting
Projects
None yet
Development

No branches or pull requests

4 participants