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

isort: bug in sorting sub-package modules #3059

Closed
spaceone opened this issue Feb 20, 2023 · 5 comments · Fixed by #3768
Closed

isort: bug in sorting sub-package modules #3059

spaceone opened this issue Feb 20, 2023 · 5 comments · Fixed by #3768
Labels
isort Related to import sorting

Comments

@spaceone
Copy link
Contributor

.isort.cfg

[settings]
known_first_party=foo.bar,baz
known_third_party=foo,blub,blah

valid isort-compatible code:

import sys

import blah
import blub
import foo
from foo import bar, baz

import baz
import foo.bar
from foo.bar import blah, blub

pyproject.toml:

[tool.ruff]
select = ["I"]
[tool.ruff.isort]
known-first-party = ["foo.bar", "baz"]
known-third-party = ["foo", "blub", "blah"]
$ ruff --fix --diff foo.py
--- foo.py
+++ foo.py
@@ -3,8 +3,8 @@
 import blah
 import blub
 import foo
+import foo.bar
 from foo import bar, baz
+from foo.bar import blah, blub
 
 import baz
-import foo.bar
-from foo.bar import blah, blub

Would fix 1 error.
@spaceone spaceone changed the title isort: bugs in sorting third party modules isort: bug in sorting sub-package modules Feb 20, 2023
@charliermarsh
Copy link
Member

I'm tempted to call this unsupported. Isn't the issue here that you're marking a module and one of its submodules as third- and first-party respectively?

@spaceone
Copy link
Contributor Author

ruff should be isort compatible, right?

we have a main package foo with many different sub-packages as foo.bar or foo.blub.
In the code of foo.bar we want that foo.bar is a first-party module and everything else (foo and foo.blub) is just a third-party module.

@charliermarsh charliermarsh added the isort Related to import sorting label Feb 20, 2023
@charliermarsh
Copy link
Member

The intent is to be isort-compatible, but we do deviate in a small number of cases, and this to me seems like a situation where there isn't a clear definition of correct behavior. I'd be open to doing what isort does rather than our current behavior, but I won't personally prioritize it.

spaceone added a commit to spaceone/ruff that referenced this issue Feb 20, 2023
e.g. packages named "foo.bar"

additionally make sure `__future__` imports are always sorted at the
very top.

Issue astral-sh#3059
@spaceone
Copy link
Contributor Author

I provided a fix in #3064. Isort has a test case for it: https://github.com/PyCQA/isort/blob/main/tests/unit/test_isort.py#L2213-L2304test_placement_control() and test_custom_sections()

spaceone added a commit to spaceone/ruff that referenced this issue Feb 20, 2023
e.g. packages named "foo.bar"

additionally make sure `__future__` imports are always sorted at the
very top.

Issue astral-sh#3059
spaceone added a commit to spaceone/ruff that referenced this issue Feb 22, 2023
e.g. packages named "foo.bar"

additionally make sure `__future__` imports are always sorted at the
very top.

Issue astral-sh#3059
spaceone added a commit to spaceone/ruff that referenced this issue Feb 22, 2023
e.g. packages named "foo.bar"

additionally make sure `__future__` imports are always sorted at the
very top.

Issue astral-sh#3059
@astaric
Copy link
Contributor

astaric commented Mar 27, 2023

One usecase where we encounter the same issue are namespace packages. We have different packages (in different repositories) share the same namespace package.

With the following config, isort lists namespace.packageA imports in the last section while all other namespace imports are listed in the third party section above.

known_first_party = namespace.packageA
default_section = THIRDPARTY

I played around with known_first_party and known_third_party options but I cannot get packages with the same namespace prefix to end up in different sections.

astaric added a commit to astaric/ruff that referenced this issue Mar 28, 2023
Allow submodules to be specified in know_first_party and
known_third_party config options.

When two entries in config options share the same prefix, the longest
match wins.

Fixes astral-sh#3059
astaric added a commit to astaric/ruff that referenced this issue Mar 28, 2023
Allow submodules to be specified in know_first_party and
known_third_party config options.

When two entries in config options share the same prefix, the longest
match wins.

Fixes astral-sh#3059
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
3 participants