separate_packages splits imports from the same explicitly configured package

Aperta Adatta ai principianti
#2,812 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@rahul05ranjan ci sta già lavorando.

Dal 25/11/2026.

  • #2814 di @rahul05ranjan — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
devtools

Direzione di ricerca

Il bug è in _separate_packages() in isort/output.py, dove package_depth viene calcolato contando i punti nella stringa del motivo di corrispondenza, includendo erroneamente il punto di re.compile. Inizia riproducendo i due esempi pycon della issue con isort.code e separate_packages impostato su THIRDPARTY. È completato quando from pkg import a e from pkg.sub import b finiscono nello stesso gruppo e l'esempio dj non configurato si comporta ancora allo stesso modo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
7k
Fork
838
Merge medio
2g 6h
PR unite (30g)
19

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PyCQA/isort

Tutte le issue di PyCQA/isort

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.