separate_packages splits imports from the same explicitly configured package

Open Beginner friendly
#2,812 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@rahul05ranjan is already working on this.

Since Nov 25, 2026.

  • #2814 by @rahul05ranjan — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
devtools

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PyCQA/isort

All issues in PyCQA/isort

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.