separate_packages splits imports from the same explicitly configured package
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
The bug is in _separate_packages() in isort/output.py, where package_depth is computed by counting dots in the match reason string, which wrongly includes the dot in re.compile. Start by reproducing the two pycon examples from the issue with isort.code and separate_packages set to THIRDPARTY. Done when from pkg import a and from pkg.sub import b land in one group, and the unconfigured dj example still behaves the same.
Written by the indexing model from the issue text.
Description
Description
The separate_packages option inserts spurious blank lines between imports
that belong to the same package, whenever that package is explicitly listed
in a known_* configuration option. The grouping only works by accident for
packages that were not explicitly configured.
Steps to reproduce
With isort 9.0.2:
>>> import isort
>>> print(isort.code(
... "from pkg import a\nfrom pkg.sub import b\n",
... separate_packages=["THIRDPARTY"],
... known_third_party=["pkg"],
... ))
from pkg import a
from pkg.sub import b
and with straight imports:
>>> print(isort.code(
... "import pkg.a\nimport pkg.b\n",
... separate_packages=["THIRDPARTY"],
... known_third_party=["pkg"],
... ))
import pkg.a
import pkg.b
For comparison, the same shapes with an unconfigured package are grouped
correctly on one package:
>>> print(isort.code(
... "from dj import a\nfrom dj.sub import b\nimport dj.x\nimport dj.y\n",
... separate_packages=["THIRDPARTY"],
... known_third_party=["dj2"],
... ))
import dj.x
import dj.y
from dj import a
from dj.sub import b
Actual behavior
from pkg import a and from pkg.sub import b (same package pkg) are split
into two groups; likewise import pkg.a / import pkg.b.
Expected behavior
Imports from the same package form one group:
from pkg import a
from pkg.sub import b
This also matters for the original use case of grouping by package within a
configured section, e.g. known_firstparty=["src"]: every module under src
currently gets its own "package" group (src.pkg, src.other, ...) instead
of being grouped under src.
Suspected cause
_separate_packages() in isort/output.py derives the package-key depth
for matched known patterns by counting dots in the reason string:
package_depth = len(reason.split(".")) - 1 # minus 1 for re.compile
The reason for a known match is
Matched configured known pattern re.compile('^pkg$'). Splitting that on
. yields two parts, so package_depth becomes 1 even though the matched
pattern ^pkg$ contains a single segment. The dot used to subtract
re.compile is counted as a pattern segment, which inflates the depth by one
for every configured pattern: the key for pkg.a/pkg.b becomes
pkg.a/pkg.b instead of pkg. Multi-segment patterns (pkg.sub) are
inflated the same way, so the grouping breaks for any explicitly configured
package.
- Dominant language
- Python
- Stars
- 7k
- Forks
- 838
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 19
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PyCQA/isort
-
force_sort_within_sections does not sort lazy imports by module namePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 2 days
-
length_sort with native sort order puts a longer import firstPossibly taken @simpleqt claimed this 24 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
PyCQA/isort#2698 · 2 comments ·
Maintainers usually reply within 2 days
-
Cache compiled known-module patterns across Config instancesPossibly taken @Yin123-ybh claimed this 2 days ago. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
PyCQA/isort#2805 · 1 comment ·
Maintainers usually reply within 2 days
-
add_imports re-adds the import on every run when the file contains an `# isort: off` block (not idempotent)Possibly taken @Tanvi-007 claimed this 6 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 72/100
PyCQA/isort#2799 · 2 comments ·
Maintainers usually reply within 2 days
-
multi_line_output=1 with include_trailing_comma leaves a dangling comma on a single importPossibly taken @beduldul claimed this 29 days ago. Open
Difficulty 1/5 1-3 hours Newbie friendliness 15/100
PyCQA/isort#2752 · 1 comment ·
Maintainers usually reply within 2 days
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
TigreGotico/text2spoken#626 ·
Maintainers usually reply within 1 day